LeetCode 1106: Parsing A Boolean Expression Solution

Master LeetCode problem 1106 (Parsing A Boolean Expression), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1106. Parsing A Boolean Expression

Problem Explanation

Explanation:

To solve this problem, we can use a recursive approach to parse the boolean expression. We will define a recursive function that evaluates the given boolean expression based on the rules provided in the problem description. We will handle the different cases for evaluating '!', '&', and '|' operations along with the base cases for 't' and 'f' literals.

Algorithm:

  1. Create a recursive function evaluate that takes the boolean expression string and an index pointer as parameters.
  2. If the current character is a '!', evaluate the inner expression and negate the result.
  3. If the current character is a '&', evaluate all inner expressions with logical AND.
  4. If the current character is a '|', evaluate all inner expressions with logical OR.
  5. For base cases, return true for 't' and false for 'f'.
  6. Update the index pointer while traversing the expression string.
  7. Return the final result after evaluating the entire expression.

Time Complexity: O(N), where N is the length of the expression string. Space Complexity: O(N), for the recursive call stack.

:

Solution Code

class Solution {
    public boolean parseBoolExpr(String expression) {
        return evaluate(expression, 0).value;
    }
    
    private Result evaluate(String expression, int start) {
        char op = expression.charAt(start);
        if (op == 't' || op == 'f') {
            return new Result(op == 't');
        } else if (op == '!') {
            start += 2; // Skip '!' and '('
            Result result = evaluate(expression, start);
            return new Result(!result.value, start + 1); // Skip ')'
        } else {
            boolean isAnd = op == '&';
            start += 2; // Skip '&' or '|', and '('
            boolean value = isAnd ? true : false; // Initialize with true for '&' and false for '|'
            while (expression.charAt(start) != ')') {
                Result subResult = evaluate(expression, start);
                if (isAnd) {
                    value &= subResult.value;
                } else {
                    value |= subResult.value;
                }
                start = subResult.nextIndex + 1;
                if (expression.charAt(start) == ',') {
                    start++; // Skip ','
                }
            }
            return new Result(value, start + 1); // Skip ')'
        }
    }
    
    class Result {
        boolean value;
        int nextIndex;
        
        public Result(boolean value) {
            this.value = value;
        }
        
        public Result(boolean value, int nextIndex) {
            this.value = value;
            this.nextIndex = nextIndex;
        }
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1106 (Parsing A Boolean Expression)?

This page provides optimized solutions for LeetCode problem 1106 (Parsing A Boolean Expression) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1106 (Parsing A Boolean Expression)?

The time complexity for LeetCode 1106 (Parsing A Boolean Expression) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1106 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1106 in Java, C++, or Python.

Back to LeetCode Solutions