diff --git a/dsa/cpp/Parsing_A_Boolean_Expression.cpp b/dsa/cpp/Parsing_A_Boolean_Expression.cpp new file mode 100644 index 0000000..46cd112 --- /dev/null +++ b/dsa/cpp/Parsing_A_Boolean_Expression.cpp @@ -0,0 +1,48 @@ +/* +Name :- Vicky +Github Id :- vicky925 +Problem Link :- https://leetcode.com/problems/parsing-a-boolean-expression/ +*/ + + + +class Solution { +public: + char evaluate(stack&s){ + bool has_true=false; + bool has_false=false; + while(s.top()!='&' && s.top()!='!' && s.top()!='|'){ + if(s.top()=='t') + has_true=true; + else + has_false=true; + s.pop(); + } + if(s.top()=='&'){ + if(has_false==false) + return 't'; + } + else if(s.top()=='!'){ + if(has_false==true) + return 't'; + } + else{ + if(has_true==true) + return 't'; + } + return 'f'; + } + bool parseBoolExpr(string expression) { + stacks; + for(auto ch:expression){ + if(ch!=')' && ch!='(' && ch!=',') + s.push(ch); + if(ch==')'){ + char temp=evaluate(s); + s.pop(); + s.push(temp); + } + } + return s.top()=='t'?true:false; + } +};