-
Notifications
You must be signed in to change notification settings - Fork 12
/
1106. Parsing A Boolean Expression
45 lines (38 loc) · 1.09 KB
/
1106. Parsing A Boolean Expression
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
class Solution {
public:
bool parseBoolExpr(string expression) {
stack<char> st, op;
for(auto & it: expression){
if(it == '&' || it == '|' || it == '!'){
op.push(it);
}
else if(it == ')'){
int f = 0, t = 0;
while(st.top() != '('){
char ch = st.top();
if(ch == 'f') f++;
if(ch == 't') t++;
st.pop();
}
st.pop();
if(op.top() == '&'){
if(f == 0) st.push('t');
else st.push('f');
}
else if(op.top() == '|'){
if(t > 0) st.push('t');
else st.push('f');
}
else{
if(f == 0) st.push('f');
else st.push('t');
}
op.pop();
}
else{
st.push(it);
}
}
return st.top() == 'f' ? false : true;
}
};