Skip to content

Practice Minimum Cost to Change the Final Value of Expression

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1896
LeetCode Problem

Minimum Cost to Change the Final Value of Expression

Problem Statement

The valid expression contains 0, 1, &, |, and parentheses. Evaluate parentheses first, then AND and OR from left to right with equal precedence. One edit may flip a digit or swap & with |; parentheses cannot be edited. Return the fewest edits needed to change the final boolean value.

Source: Minimum Cost to Change the Final Value of Expression on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
expression = "1&(0|1)"
Output
1

# Example 2

Input
expression = "(0&0)&(0&0&0)"
Output
3

# Example 3

Input
expression = "(0|(1|0&1))"
Output
1

Constraints

1 <= expression.length <= 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).

Related LeetCode Problems

Practice more problems similar to Minimum Cost to Change the Final Value of Expression.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.