|
| 1 | +# 1106. Parsing A Boolean Expression |
| 2 | +A **boolean expression** is an expression that evaluates to either `true` or `false`. It can be in one of the following shapes: |
| 3 | + |
| 4 | +* `'t'` that evaluates to `true`. |
| 5 | +* `'f'` that evaluates to `false`. |
| 6 | +* `'!(subExpr)'` that evaluates to **the logical NOT** of the inner expression `subExpr`. |
| 7 | +* `'&(subExpr1, subExpr2, ..., subExprn)'` that evaluates to **the logical AND** of the inner expressions `subExpr1, subExpr2, ..., subExprn` where `n >= 1`. |
| 8 | +* `'|(subExpr1, subExpr2, ..., subExprn)'` that evaluates to **the logical OR** of the inner expressions `subExpr1, subExpr2, ..., subExprn` where `n >= 1`. |
| 9 | + |
| 10 | +Given a string `expression` that represents a **boolean expression**, return *the evaluation of that expression*. |
| 11 | + |
| 12 | +It is **guaranteed** that the given expression is valid and follows the given rules. |
| 13 | + |
| 14 | +#### Example 1: |
| 15 | +<pre> |
| 16 | +<strong>Input:</strong> expression = "&(|(f))" |
| 17 | +<strong>Output:</strong> false |
| 18 | +<strong>Explanation:</strong> |
| 19 | +First, evaluate |(f) --> f. The expression is now "&(f)". |
| 20 | +Then, evaluate &(f) --> f. The expression is now "f". |
| 21 | +Finally, return false. |
| 22 | +</pre> |
| 23 | + |
| 24 | +#### Example 2: |
| 25 | +<pre> |
| 26 | +<strong>Input:</strong> expression = "|(f,f,f,t)" |
| 27 | +<strong>Output:</strong> true |
| 28 | +<strong>Explanation:</strong> The evaluation of (false OR false OR false OR true) is true. |
| 29 | +</pre> |
| 30 | + |
| 31 | +#### Example 3: |
| 32 | +<pre> |
| 33 | +<strong>Input:</strong> expression = "!(&(f,t))" |
| 34 | +<strong>Output:</strong> true |
| 35 | +<strong>Explanation:</strong> |
| 36 | +First, evaluate &(f,t) --> (false AND true) --> false --> f. The expression is now "!(f)". |
| 37 | +Then, evaluate !(f) --> NOT false --> true. We return true. |
| 38 | +</pre> |
| 39 | + |
| 40 | +#### Constraints: |
| 41 | +* <code>1 <= expression.length <= 2 * 10<sup>4</sup></code> |
| 42 | +* expression[i] is one following characters: `'('`, `')'`, `'&'`, `'|'`, `'!'`, `'t'`, `'f'`, and `','`. |
| 43 | + |
| 44 | +## Solutions (Rust) |
| 45 | + |
| 46 | +### 1. Solution |
| 47 | +```Rust |
| 48 | +impl Solution { |
| 49 | + pub fn parse_bool_expr(expression: String) -> bool { |
| 50 | + let mut ops = vec![]; |
| 51 | + let mut bools = vec![]; |
| 52 | + |
| 53 | + for ch in expression.chars() { |
| 54 | + match ch { |
| 55 | + '!' | '&' | '|' => ops.push(ch), |
| 56 | + 't' | 'f' => bools.push(Some(ch == 't')), |
| 57 | + ')' => match ops.pop() { |
| 58 | + Some('!') => { |
| 59 | + let tmp = !bools.pop().unwrap().unwrap(); |
| 60 | + bools.pop(); |
| 61 | + bools.push(Some(tmp)); |
| 62 | + } |
| 63 | + Some('&') => { |
| 64 | + let mut tmp = true; |
| 65 | + while let Some(Some(b)) = bools.pop() { |
| 66 | + tmp &= b; |
| 67 | + } |
| 68 | + bools.push(Some(tmp)); |
| 69 | + } |
| 70 | + Some('|') => { |
| 71 | + let mut tmp = false; |
| 72 | + while let Some(Some(b)) = bools.pop() { |
| 73 | + tmp |= b; |
| 74 | + } |
| 75 | + bools.push(Some(tmp)); |
| 76 | + } |
| 77 | + _ => { |
| 78 | + let tmp = bools.pop().unwrap(); |
| 79 | + bools.pop(); |
| 80 | + bools.push(tmp); |
| 81 | + } |
| 82 | + }, |
| 83 | + '(' => bools.push(None), |
| 84 | + _ => (), |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + bools[0].unwrap() |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
0 commit comments