We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 225dd30 + ae90f7b commit 679cb42Copy full SHA for 679cb42
1106. Parsing A Boolean Expression
@@ -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
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
31
32
+ else{
33
+ if(f == 0) st.push('f');
34
+ else st.push('t');
35
36
+ op.pop();
37
38
39
+ st.push(it);
40
41
42
43
+ return st.top() == 'f' ? false : true;
44
45
+};
0 commit comments