-
Notifications
You must be signed in to change notification settings - Fork 0
/
1106. Parsing A Boolean Expression.java
44 lines (37 loc) · 1.84 KB
/
1106. Parsing A Boolean Expression.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
public boolean parseBoolExpr(String expression) {
Stack<Character> stk = new Stack<>(); // Stack to hold characters and operators
// Iterate over each character in the expression
for (char c : expression.toCharArray()) {
// Push valid characters (non ')' and non ',') to the stack
if (c != ')' && c != ',') stk.push(c);
else if (c == ')') { // When ')' is encountered, evaluate subexpression
ArrayList<Boolean> exp = new ArrayList<>(); // List to hold boolean values
// Pop characters until '(' is found, collect 't' or 'f' values
while (!stk.isEmpty() && stk.peek() != '(') {
char t = stk.pop();
if (t == 't') exp.add(true);
else exp.add(false);
}
stk.pop(); // Pop the '(' from the stack
if (!stk.isEmpty()) {
char t = stk.pop(); // Get the operator before '('
boolean v = exp.get(0); // Initialize result with the first value
// Perform the corresponding logical operation
if (t == '&') { // AND operation
for (boolean b : exp) v &= b;
} else if (t == '|') { // OR operation
for (boolean b : exp) v |= b;
} else { // NOT operation
v = !v;
}
// Push the result back to the stack as 't' or 'f'
if (v) stk.push('t');
else stk.push('f');
}
}
}
// Return the final result from the stack
return stk.peek() == 't';
}
}