From ae90f7bfad173bbf698dddc0ce3b1aba830c5769 Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Sun, 20 Oct 2024 19:33:33 +0530 Subject: [PATCH] Create 1106. Parsing A Boolean Expression --- 1106. Parsing A Boolean Expression | 45 ++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 1106. Parsing A Boolean Expression diff --git a/1106. Parsing A Boolean Expression b/1106. Parsing A Boolean Expression new file mode 100644 index 0000000..ad28e27 --- /dev/null +++ b/1106. Parsing A Boolean Expression @@ -0,0 +1,45 @@ +class Solution { +public: + bool parseBoolExpr(string expression) { + + stack 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; + } +};