This repository contains multiple implementations of the solution for the LeetCode problem Parsing a Boolean Expression. The problem requires parsing and evaluating a Boolean expression based on the given rules for logical operators AND
, OR
, and NOT
. Below is a step-by-step explanation for how the solution is designed and implemented in each language.
We are given a boolean expression that evaluates either to true
or false
. The expression can be:
t
which evaluates totrue
.f
which evaluates tofalse
.!(subExpr)
which evaluates to the logical NOT of the inner expressionsubExpr
.&(subExpr1, subExpr2, ..., subExprn)
which evaluates to the logical AND of the inner expressions.|(subExpr1, subExpr2, ..., subExprn)
which evaluates to the logical OR of the inner expressions.
Our goal is to evaluate the expression and return the result as either true
or false
.
-
Stack-based Solution:
- We traverse the expression character by character.
- When we encounter a closing parenthesis
)
, it signals the end of an inner expression. We pop all the elements inside the parentheses, apply the logical operator (!
,&
, or|
), and push the result back onto the stack. - We continue this process until we finish processing the entire string.
-
Operators and Logic:
!
(NOT): Inverts the truth value of the sub-expression.&
(AND): Returnstrue
if all the sub-expressions aretrue
, otherwise returnsfalse
.|
(OR): Returnstrue
if at least one sub-expression istrue
.
-
Initialize the Stack:
- A stack is used to keep track of the characters in the expression.
- We process each character from the input expression string one by one.
-
Process Characters:
- If a closing parenthesis
)
is encountered, start collecting the sub-expressions within the parentheses until the opening parenthesis(
is found. - Determine the operator (
!
,&
, or|
) immediately before the parentheses.
- If a closing parenthesis
-
Evaluate Sub-expressions:
- For
!
: Apply the NOT operation on the single sub-expression. - For
&
: Apply the AND operation across all sub-expressions. - For
|
: Apply the OR operation across all sub-expressions.
- For
-
Push Results Back to Stack:
- After evaluating, push the result (
t
orf
) back onto the stack.
- After evaluating, push the result (
-
Final Result:
- After the entire expression is processed, the top of the stack contains the final result, which is either
true
orfalse
.
- After the entire expression is processed, the top of the stack contains the final result, which is either
-
Stack Initialization:
- A stack is initialized to store characters during the traversal of the input expression.
-
Handling Parentheses:
- When a closing parenthesis
)
is encountered, we collect the characters from the stack until the corresponding opening parenthesis(
is found. - The operator that precedes the opening parenthesis determines how the sub-expressions should be evaluated.
- When a closing parenthesis
-
Logical Operations:
- For
!
(NOT), we invert the truth value of the sub-expression. - For
&
(AND), we check if all sub-expressions evaluate totrue
; otherwise, the result isfalse
. - For
|
(OR), we returntrue
if at least one sub-expression evaluates totrue
.
- For
-
Final Push:
- After evaluating the sub-expressions, we push the result back onto the stack for further use.
-
Returning the Result:
- Once the entire expression is processed, the top of the stack gives the final result of the Boolean expression.
-
Use of Stack:
- A stack is used to track the characters and sub-expressions within the Boolean expression as we iterate through it.
-
Detecting Sub-expressions:
- When encountering a
)
, gather all the sub-expressions inside the parentheses and determine the operator (!
,&
, or|
).
- When encountering a
-
Evaluating the Expression:
- Apply the operator to the collected sub-expressions:
- For
!
, invert the truth value. - For
&
, ensure all sub-expressions aretrue
to result intrue
. - For
|
, check if any sub-expression istrue
.
- For
- Apply the operator to the collected sub-expressions:
-
Update Stack:
- Push the evaluation result back to the stack.
-
Return Final Boolean:
- After traversing the entire input, the Boolean result is at the top of the stack and is returned.
-
Stack for Processing:
- Initialize a stack to manage the characters of the expression.
- Each character is processed sequentially.
-
Sub-expression Handling:
- Upon encountering
)
, gather the sub-expressions within the parentheses and then evaluate them based on the operator right before(
.
- Upon encountering
-
Boolean Logic Application:
- Apply the corresponding operator:
- For
!
, invert the value. - For
&
, check that all sub-expressions aretrue
. - For
|
, returntrue
if at least one sub-expression istrue
.
- For
- Apply the corresponding operator:
-
Push Result:
- After evaluation, push the resulting value back to the stack.
-
Final Result:
- At the end of the iteration, the stack will contain the final result of the Boolean expression, which is returned.
-
Stack Initialization:
- Use a stack to keep track of characters and sub-expressions as we parse through the Boolean expression.
-
Processing Characters:
- When a
)
is found, pop elements from the stack until we reach the corresponding(
. - Use the operator found before
(
to evaluate the sub-expressions.
- When a
-
Evaluate Expression:
- Based on the operator:
- For
!
, apply NOT on the single sub-expression. - For
&
, apply AND across all sub-expressions. - For
|
, apply OR across all sub-expressions.
- For
- Based on the operator:
-
Push Back Result:
- After evaluating, push the result (
t
orf
) back to the stack.
- After evaluating, push the result (
-
Final Output:
- The result of the Boolean expression is found at the top of the stack at the end of the process.
In all the implementations, the key steps involve:
- Stack-based parsing: Using a stack to manage characters and sub-expressions.
- Operator handling: Determining the logical operation to apply (
!
,&
,|
). - Sub-expression evaluation: Processing sub-expressions within parentheses and pushing the results back to the stack.
- Final result: The top of the stack contains the Boolean result after processing the entire expression.
-
Time Complexity:
$$O(n)$$ , where (n) is the length of the expression. Each character in the string is processed once, and the sub-expressions are evaluated linearly.
-
Space Complexity:
$$O(n)$$ , as we use a stack to store intermediate characters and results during the parsing process.
By following this step-by-step guide, you will understand how the Boolean expression is parsed and evaluated using a stack-based approach across multiple programming languages.