-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredundantBrackets.cpp
55 lines (51 loc) · 953 Bytes
/
redundantBrackets.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
#include <iostream>
#include <stack>
using namespace std;
void print(stack<char>& s) {
if (s.empty()) return;
char last = s.top();
s.pop();
print(s);
s.push(last);
cout << s.top();
}
void hasRedundantBracket(stack<char>& s, bool* flag, int count = 0) {
if (count == 2) {
*flag = true;
return;
}
if (s.empty()) {
return;
}
char removed = s.top();
s.pop();
if (s.empty()) {
return;
}
char top = s.top();
if (top == removed && (top == ')' || top == '(')) {
count++;
}
hasRedundantBracket(s, flag, count);
s.push(top);
}
int main() {
stack<char> s;
bool* flag = new bool(false);
s.push('(');
s.push('b');
s.push('+');
s.push('(');
s.push('b');
s.push('+');
s.push('a');
s.push(')');
s.push(')');
print(s);
cout << endl;
hasRedundantBracket(s, flag, 0);
cout << "Has redundant brackets: ";
(*flag) ? cout << "true" : cout << "false";
cout << endl;
return 0;
}