You are given a valid boolean expression as a string expression consisting of the characters '1','0','&' (bitwise AND operator),'|' (bitwise OR operator),'(', and ')'.
For example, "()1|1" and "(1)&()" are not valid while "1", "(((1))|(0))", and "1|(0&(1))" are valid expressions.
Return the minimum cost to change the final value of the expression.
For example, if expression = "1|1|(0&0)&1", its value is 1|1|(0&0)&1 = 1|1|0&1 = 1|0&1 = 1&1 = 1. We want to apply operations so that the new expression evaluates to 0.
The cost of changing the final value of an expression is the number of operations performed on the expression. The types of operations are described as follows:
Turn a '1' into a '0'.
Turn a '0' into a '1'.
Turn a '&' into a '|'.
Turn a '|' into a '&'.
Note:'&' does not take precedence over '|' in the order of calculation. Evaluate parentheses first, then in left-to-right order.
Example 1:
Input: expression = "1&(0|1)"
Output: 1
Explanation: We can turn "1&(0|1)" into "1&(0&1)" by changing the '|' to a '&' using 1 operation.
The new expression evaluates to 0.
Example 2:
Input: expression = "(0&0)&(0&0&0)"
Output: 3
Explanation: We can turn "(0&0)&(0&0&0)" into "(0|1)|(0&0&0)" using 3 operations.
The new expression evaluates to 1.
Example 3:
Input: expression = "(0|(1|0&1))"
Output: 1
Explanation: We can turn "(0|(1|0&1))" into "(0|(0|0&1))" using 1 operation.
The new expression evaluates to 0.
Constraints:
1 <= expression.length <= 105
expression only contains '1','0','&','|','(', and ')'
All parentheses are properly matched.
There will be no empty parentheses (i.e: "()" is not a substring of expression).
Solutions
Solution 1
Thinking
A valid boolean expression uses \(0/1\), \(\&\), \(|\), and parentheses. One edit flips a digit or an operator. The expression can have length \(10^5\), so reevaluating every edit is impossible.
Each subexpression only needs its current value and the cost of flipping it. A leaf costs \(1\) to flip. When two sides are joined by \(\&\) or \(|\), the flip cost follows from changing the operator, one child, or both. A stack parses parentheses and operators and merges these pairs from the bottom up.
Represent each subexpression by \((\textit{val},\textit{cost})\): its current boolean value and the minimum edits that flip it. A digit costs \(1\) to flip.
\(\&\) and \(|\) have the same precedence and associate left to right; parentheses bind tighter. Two stacks store subexpressions and operators. An operator reduces any pending operator of the same precedence, and a closing parenthesis reduces until the matching opening parenthesis.
Let the two sides be \((v_1,c_1)\) and \((v_2,c_2)\).
For \(\&\) with both sides \(1\), the value is \(1\) and the flip cost is \(\min(c_1,c_2)\).
With both sides \(0\), the value is \(0\). Flipping both operands costs \(c_1+c_2\); changing \(\&\) to \(|\) and flipping one operand costs \(1+\min(c_1,c_2)\).
With exactly one \(0\), the value is \(0\). Flip that \(0\), or change \(\&\) to \(|\), and take the cheaper cost.
The three cases for \(|\) are dual to the cases above.
After the expression is reduced, the top \(\textit{cost}\) is the answer.
The time complexity is \(O(n)\) and the space complexity is \(O(n)\), where \(n\) is the length of the expression.