class Solution {
public boolean parseBoolExpr(String expression) {
int len = expression.length();
Stack<Character> st = new Stack<>();
for (int i = 0; i < len; i++) {
if (expression.charAt(i) == '(' || expression.charAt(i)==',') continue;
else if (expression.charAt(i) == ')') {
boolean mark_f = false, mark_t = false;
while (!st.isEmpty() && (st.peek() == 't' || st.peek() == 'f')) {
if (st.peek() == 't') mark_t = true;
if (st.peek() == 'f') mark_f = true;
st.pop();
}
char c = st.pop();
if (c == '!') {
if (mark_f) st.add('t');
else st.add('f');
} else if (c == '&') {
if (mark_f) st.add('f');
else st.add('t');
} else {
if (mark_t) st.add('t');
else st.add('f');
}
} else
st.add(expression.charAt(i));
}
return st.peek() == 't' ? true : false;
}
}
public int parseBoolExpr(String expression){
Stack<Character> stack = new Stack<>();
for(int i = 0;i<expression.length(); i++){
if(expression.charAt(i) != ')'){
if(expression.charAt(i)!=','){
stack.push(expression.charAt(i));
}
}else {
StringBuilder temp = new StringBuilder();
while(stack.peek() != '('){
temp.insert(0,stack.pop());
}
stack.pop();
Character c = stack.pop();
if(c == '!'){
if(temp.toString().equals("t")){
stack.push('f');
}else {
stack.push('t');
}
}else if(c == '&'){
boolean flag = true;
if(temp.indexOf("f")!=-1){
flag = false;
}
if(flag){
stack.push('t');
}else {
stack.push('f');
}
}else {
boolean flag = false;
if(temp.indexOf("t")!=-1){
flag = true;
}
if(flag){
stack.push('t');
}else {
stack.push('f');
}
}
}
}
return stack.pop() == 't'?1:0;
}