forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParsing A Boolean Expression.cpp
43 lines (43 loc) · 1.05 KB
/
Parsing A Boolean Expression.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
class Solution {
pair<bool,int> dfs(string &e, int idx){
bool res;
if(e[idx]=='!'){
auto [a,b]=dfs(e, idx+2);
return {!a,b+3};
}else if(e[idx]=='&'){
int len=2;
res=true;
idx+=2;
while(e[idx]!=')'){
if(e[idx]==','){
idx++;len++;
}
auto [a,b]=dfs(e,idx);
res&=a;
idx+=b;
len+=b;
}
return {res,len+1};
}else if(e[idx]=='|'){
int len=2;
res=false;
idx+=2;
while(e[idx]!=')'){
if(e[idx]==','){
idx++;len++;
}
auto [a,b]=dfs(e,idx);
res|=a;
idx+=b;
len+=b;
}
return {res,len+1};
}else{
return {e[idx]=='t',1};
}
}
public:
bool parseBoolExpr(string expression) {
return dfs(expression, 0).first;
}
};