Mid/SeniorGreedyString

Minimum Suffix Flips

You are given a 0-indexed binary string target of length n. You have another binary string s of length n that is initially set to all zeros. You want to make s equal to target.

In one operation, you can pick an index i where 0 <= i < n and flip all bits in the inclusive range [i, n - 1]. Flip means changing '0' to '1' and '1' to '0'.

Return the minimum number of operations needed to make s equal to target.

Example 1
Inputtarget = "10111"
Output3
Starting from s = "00000", flipping suffixes at indices 2, 0, and 1 forms target = "10111", and at least 3 operations are needed.
Example 2
Inputtarget = "101"
Output3
Starting from s = "000", flipping suffixes at indices 0, 1, and 2 forms target = "101", and at least 3 operations are needed.

Constraints

  • n == target.length
  • 1 <= n <= 10^5
  • target[i] is either '0' or '1'.

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate