Tap here to switch tabs
Problem
Submissions

Problem: Minimum Cost to Change the Final Value of Expression

med
30 min
Try to solve the Minimum Cost to Change the Final Value of Expression problem.

Statement

You are given a valid boolean expression represented by a string expression that consists of the characters $'1'$, $'0'$, $'&'$, $'|'$, $'('$, and }$')'$.

The operators evaluate as follows:

  • $'1'$ evaluates to true, and $'0'$ evaluates to false.

  • $'&'$ represents the boolean AND operation, while $'|'$ represents the boolean OR operation.

  • Operations are evaluated from left to right, while respecting the precedence of parentheses.

In one operation, you can flip any single character:

  • Change a $'1'$ to a $'0'$ or a $'0'$ to a $'1'$.

  • Change a $'&'$ to a $'|'$ or a $'|'$ to a $'&'$.

Return the minimum number of operations required to change the final evaluated boolean value of the expression.

Constraints:

  • 1≤1 \leq expression.length ≤105\leq 10^5

  • 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).

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Cost to Change the Final Value of Expression

med
30 min
Try to solve the Minimum Cost to Change the Final Value of Expression problem.

Statement

You are given a valid boolean expression represented by a string expression that consists of the characters $'1'$, $'0'$, $'&'$, $'|'$, $'('$, and }$')'$.

The operators evaluate as follows:

  • $'1'$ evaluates to true, and $'0'$ evaluates to false.

  • $'&'$ represents the boolean AND operation, while $'|'$ represents the boolean OR operation.

  • Operations are evaluated from left to right, while respecting the precedence of parentheses.

In one operation, you can flip any single character:

  • Change a $'1'$ to a $'0'$ or a $'0'$ to a $'1'$.

  • Change a $'&'$ to a $'|'$ or a $'|'$ to a $'&'$.

Return the minimum number of operations required to change the final evaluated boolean value of the expression.

Constraints:

  • 1≤1 \leq expression.length ≤105\leq 10^5

  • 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).