Skip to content

Commit

Permalink
Time: 13 ms (46.1%), Space: 42.9 MB (55.67%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit-S-Sahu committed Oct 20, 2024
1 parent 97fec8d commit a66b6a9
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public boolean parseBoolExpr(String expression) {
Stack<Character> operator = new Stack<>();
Stack<Character> operands = new Stack<>();

for (int i = 0; i < expression.length(); i++) {
char curr = expression.charAt(i);

if (curr == '!' || curr == '&' || curr == '|') operator.push(curr);
else if (curr == '(') operands.push(curr);
else if (curr == 'f') operands.push('0');
else if (curr == 't') operands.push('1');
else if (curr == ')') {
char op = operator.pop();
int res = (operands.pop() - '0');

while (operands.peek() != '(') {
if (op == '&') res &= (operands.pop() - '0');
else if (op == '|') res |= (operands.pop() - '0');
}
operands.pop();
if (op == '!') res = res == 1 ? 0 : 1;
operands.push((char)(res + '0'));
}
}
return operands.pop() == '1';
}
}

0 comments on commit a66b6a9

Please sign in to comment.