forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
24 lines (24 loc) · 836 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public boolean parseBoolExpr(String expression) {
Deque<Character> stk = new ArrayDeque<>();
for (char c : expression.toCharArray()) {
if (c != '(' && c != ')' && c != ',') {
stk.push(c);
} else if (c == ')') {
int t = 0, f = 0;
while (stk.peek() == 't' || stk.peek() == 'f') {
t += stk.peek() == 't' ? 1 : 0;
f += stk.peek() == 'f' ? 1 : 0;
stk.pop();
}
char op = stk.pop();
c = 'f';
if ((op == '!' && f > 0) || (op == '&' && f == 0) || (op == '|' && t > 0)) {
c = 't';
}
stk.push(c);
}
}
return stk.peek() == 't';
}
}