-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1106. Parsing A Boolean Expression
More file actions
34 lines (33 loc) · 1.05 KB
/
Copy path1106. Parsing A Boolean Expression
File metadata and controls
34 lines (33 loc) · 1.05 KB
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
class Solution {
public:
bool parseBoolExpr(string expression) {
stack<char> q,qq;
for(char ch : expression) {
if(ch == ',') continue;
if(ch == '(' or ch == 'f' or ch == 't') q.push(ch);
else if(ch == ')') {
int cntTrue = 0,cntFalse = 0;
while(q.size() and q.top() != '(') {
if(q.top() == 'f') cntFalse++;
else cntTrue++;
q.pop();
}
q.pop();
char ch1 = qq.top();
qq.pop();
if(ch1 == '|') {
if(cntTrue) q.push('t');
else q.push('f');
} else if(ch1 == '&') {
if(cntFalse) q.push('f');
else q.push('t');
} else {
if(cntFalse) q.push('t');
else q.push('f');
}
} else qq.push(ch);
}
if(q.top() == 't') return true;
return false;
}
};