Skip to content

Commit 679cb42

Browse files
authored
Create 1106. Parsing A Boolean Expression (#614)
2 parents 225dd30 + ae90f7b commit 679cb42

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

1106. Parsing A Boolean Expression

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution {
2+
public:
3+
bool parseBoolExpr(string expression) {
4+
5+
stack<char> st, op;
6+
7+
for(auto & it: expression){
8+
9+
if(it == '&' || it == '|' || it == '!'){
10+
op.push(it);
11+
}
12+
else if(it == ')'){
13+
14+
int f = 0, t = 0;
15+
while(st.top() != '('){
16+
char ch = st.top();
17+
if(ch == 'f') f++;
18+
if(ch == 't') t++;
19+
st.pop();
20+
}
21+
22+
st.pop();
23+
24+
if(op.top() == '&'){
25+
if(f == 0) st.push('t');
26+
else st.push('f');
27+
}
28+
else if(op.top() == '|'){
29+
if(t > 0) st.push('t');
30+
else st.push('f');
31+
}
32+
else{
33+
if(f == 0) st.push('f');
34+
else st.push('t');
35+
}
36+
op.pop();
37+
}
38+
else{
39+
st.push(it);
40+
}
41+
}
42+
43+
return st.top() == 'f' ? false : true;
44+
}
45+
};

0 commit comments

Comments
 (0)