Skip to content

Commit

Permalink
Create 1106. Parsing A Boolean Expression
Browse files Browse the repository at this point in the history
  • Loading branch information
Chayandas07 authored Oct 20, 2024
1 parent 225dd30 commit ae90f7b
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions 1106. Parsing A Boolean Expression
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Solution {
public:
bool parseBoolExpr(string expression) {

stack<char> st, op;

for(auto & it: expression){

if(it == '&' || it == '|' || it == '!'){
op.push(it);
}
else if(it == ')'){

int f = 0, t = 0;
while(st.top() != '('){
char ch = st.top();
if(ch == 'f') f++;
if(ch == 't') t++;
st.pop();
}

st.pop();

if(op.top() == '&'){
if(f == 0) st.push('t');
else st.push('f');
}
else if(op.top() == '|'){
if(t > 0) st.push('t');
else st.push('f');
}
else{
if(f == 0) st.push('f');
else st.push('t');
}
op.pop();
}
else{
st.push(it);
}
}

return st.top() == 'f' ? false : true;
}
};

0 comments on commit ae90f7b

Please sign in to comment.