-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpression_contains_redundant_bracket_or_not.cpp
59 lines (50 loc) · 1.22 KB
/
Expression_contains_redundant_bracket_or_not.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <bits/stdc++.h>
using namespace std;
bool checkRedundancy(string &str)
{
stack<char> s;
int counter = 0;
for (int i = 0; i < str.size(); i++)
{
if (str[i] != ')')
s.push(str[i]);
else
{
// If we found an opening bracket just after closing one.
// Then the bracket pair had no content. Hence, redundant.
if(s.top() == '(')
return true;
else
{
while(s.top() != '(')
{
s.pop();
counter++;
}
s.pop(); // Removing the opening bracket.
// This basically means that bracket is around an single element only.
if(counter <= 1)
return true;
}
}
}
return false;
}
void findRedundant(string &str)
{
bool answer = checkRedundancy(str);
if (answer == true)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
int main()
{
string str = "((a+b))";
findRedundant(str);
str = "(a+(b)/c)";
findRedundant(str);
str = "(a+b*(c-d))";
findRedundant(str);
return 0;
}