|
| 1 | +<h2><a href="https://leetcode.com/problems/parsing-a-boolean-expression">1106. Parsing A Boolean Expression</a></h2><h3>Hard</h3><hr><p>A <strong>boolean expression</strong> is an expression that evaluates to either <code>true</code> or <code>false</code>. It can be in one of the following shapes:</p> |
| 2 | + |
| 3 | +<ul> |
| 4 | + <li><code>'t'</code> that evaluates to <code>true</code>.</li> |
| 5 | + <li><code>'f'</code> that evaluates to <code>false</code>.</li> |
| 6 | + <li><code>'!(subExpr)'</code> that evaluates to <strong>the logical NOT</strong> of the inner expression <code>subExpr</code>.</li> |
| 7 | + <li><code>'&(subExpr<sub>1</sub>, subExpr<sub>2</sub>, ..., subExpr<sub>n</sub>)'</code> that evaluates to <strong>the logical AND</strong> of the inner expressions <code>subExpr<sub>1</sub>, subExpr<sub>2</sub>, ..., subExpr<sub>n</sub></code> where <code>n >= 1</code>.</li> |
| 8 | + <li><code>'|(subExpr<sub>1</sub>, subExpr<sub>2</sub>, ..., subExpr<sub>n</sub>)'</code> that evaluates to <strong>the logical OR</strong> of the inner expressions <code>subExpr<sub>1</sub>, subExpr<sub>2</sub>, ..., subExpr<sub>n</sub></code> where <code>n >= 1</code>.</li> |
| 9 | +</ul> |
| 10 | + |
| 11 | +<p>Given a string <code>expression</code> that represents a <strong>boolean expression</strong>, return <em>the evaluation of that expression</em>.</p> |
| 12 | + |
| 13 | +<p>It is <strong>guaranteed</strong> that the given expression is valid and follows the given rules.</p> |
| 14 | + |
| 15 | +<p> </p> |
| 16 | +<p><strong class="example">Example 1:</strong></p> |
| 17 | + |
| 18 | +<pre> |
| 19 | +<strong>Input:</strong> expression = "&(|(f))" |
| 20 | +<strong>Output:</strong> false |
| 21 | +<strong>Explanation:</strong> |
| 22 | +First, evaluate |(f) --> f. The expression is now "&(f)". |
| 23 | +Then, evaluate &(f) --> f. The expression is now "f". |
| 24 | +Finally, return false. |
| 25 | +</pre> |
| 26 | + |
| 27 | +<p><strong class="example">Example 2:</strong></p> |
| 28 | + |
| 29 | +<pre> |
| 30 | +<strong>Input:</strong> expression = "|(f,f,f,t)" |
| 31 | +<strong>Output:</strong> true |
| 32 | +<strong>Explanation:</strong> The evaluation of (false OR false OR false OR true) is true. |
| 33 | +</pre> |
| 34 | + |
| 35 | +<p><strong class="example">Example 3:</strong></p> |
| 36 | + |
| 37 | +<pre> |
| 38 | +<strong>Input:</strong> expression = "!(&(f,t))" |
| 39 | +<strong>Output:</strong> true |
| 40 | +<strong>Explanation:</strong> |
| 41 | +First, evaluate &(f,t) --> (false AND true) --> false --> f. The expression is now "!(f)". |
| 42 | +Then, evaluate !(f) --> NOT false --> true. We return true. |
| 43 | +</pre> |
| 44 | + |
| 45 | +<p> </p> |
| 46 | +<p><strong>Constraints:</strong></p> |
| 47 | + |
| 48 | +<ul> |
| 49 | + <li><code>1 <= expression.length <= 2 * 10<sup>4</sup></code></li> |
| 50 | + <li>expression[i] is one following characters: <code>'('</code>, <code>')'</code>, <code>'&'</code>, <code>'|'</code>, <code>'!'</code>, <code>'t'</code>, <code>'f'</code>, and <code>','</code>.</li> |
| 51 | +</ul> |
0 commit comments