|
4 | 4 |
|
5 | 5 | ## Description
|
6 | 6 |
|
7 |
| -<p>Return the result of evaluating a given boolean <code>expression</code>, represented as a string.</p> |
8 |
| - |
9 |
| -<p>An expression can either be:</p> |
| 7 | +<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> |
10 | 8 |
|
11 | 9 | <ul>
|
12 |
| - <li><code>"t"</code>, evaluating to <code>True</code>;</li> |
13 |
| - <li><code>"f"</code>, evaluating to <code>False</code>;</li> |
14 |
| - <li><code>"!(expr)"</code>, evaluating to the logical NOT of the inner expression <code>expr</code>;</li> |
15 |
| - <li><code>"&(expr1,expr2,...)"</code>, evaluating to the logical AND of 2 or more inner expressions <code>expr1, expr2, ...</code>;</li> |
16 |
| - <li><code>"|(expr1,expr2,...)"</code>, evaluating to the logical OR of 2 or more inner expressions <code>expr1, expr2, ...</code></li> |
| 10 | + <li><code>'t'</code> that evaluates to <code>true</code>.</li> |
| 11 | + <li><code>'f'</code> that evaluates to <code>false</code>.</li> |
| 12 | + <li><code>'!(subExpr)'</code> that evaluates to <strong>the logical NOT</strong> of the inner expression <code>subExpr</code>.</li> |
| 13 | + <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> |
| 14 | + <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> |
17 | 15 | </ul>
|
18 | 16 |
|
| 17 | +<p>Given a string <code>expression</code> that represents a <strong>boolean expression</strong>, return <em>the evaluation of that expression</em>.</p> |
| 18 | + |
| 19 | +<p>It is <strong>guaranteed</strong> that the given expression is valid and follows the given rules.</p> |
| 20 | + |
19 | 21 | <p> </p>
|
20 |
| -<p><strong>Example 1:</strong></p> |
| 22 | +<p><strong class="example">Example 1:</strong></p> |
21 | 23 |
|
22 | 24 | <pre>
|
23 |
| -<strong>Input:</strong> expression = "!(f)" |
24 |
| -<strong>Output:</strong> true |
| 25 | +<strong>Input:</strong> expression = "&(|(f))" |
| 26 | +<strong>Output:</strong> false |
| 27 | +<strong>Explanation:</strong> |
| 28 | +First, evaluate |(f) --> f. The expression is now "&(f)". |
| 29 | +Then, evaluate &(f) --> f. The expression is now "f". |
| 30 | +Finally, return false. |
25 | 31 | </pre>
|
26 | 32 |
|
27 |
| -<p><strong>Example 2:</strong></p> |
| 33 | +<p><strong class="example">Example 2:</strong></p> |
28 | 34 |
|
29 | 35 | <pre>
|
30 |
| -<strong>Input:</strong> expression = "|(f,t)" |
| 36 | +<strong>Input:</strong> expression = "|(f,f,f,t)" |
31 | 37 | <strong>Output:</strong> true
|
| 38 | +<strong>Explanation:</strong> The evaluation of (false OR false OR false OR true) is true. |
32 | 39 | </pre>
|
33 | 40 |
|
34 |
| -<p><strong>Example 3:</strong></p> |
| 41 | +<p><strong class="example">Example 3:</strong></p> |
35 | 42 |
|
36 | 43 | <pre>
|
37 |
| -<strong>Input:</strong> expression = "&(t,f)" |
38 |
| -<strong>Output:</strong> false |
| 44 | +<strong>Input:</strong> expression = "!(&(f,t))" |
| 45 | +<strong>Output:</strong> true |
| 46 | +<strong>Explanation:</strong> |
| 47 | +First, evaluate &(f,t) --> (false AND true) --> false --> f. The expression is now "!(f)". |
| 48 | +Then, evaluate !(f) --> NOT false --> true. We return true. |
39 | 49 | </pre>
|
40 | 50 |
|
41 | 51 | <p> </p>
|
42 | 52 | <p><strong>Constraints:</strong></p>
|
43 | 53 |
|
44 | 54 | <ul>
|
45 | 55 | <li><code>1 <= expression.length <= 2 * 10<sup>4</sup></code></li>
|
46 |
| - <li><code>expression[i]</code> consists of characters in <code>{'(', ')', '&', '|', '!', 't', 'f', ','}</code>.</li> |
47 |
| - <li><code>expression</code> is a valid expression representing a boolean, as given in the description.</li> |
| 56 | + <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> |
48 | 57 | </ul>
|
49 | 58 |
|
50 | 59 | ## Solutions
|
|
0 commit comments