Minimum Suffix Flips - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
target = "10111"
›
Output:
3
💡 Note:
Start with "00000". Flip at index 0 → "11111". Flip at index 1 → "10000". Flip at index 4 → "10001". Continue until we get "10111" with 3 total operations.
Example 2 — All Zeros
$
Input:
target = "000"
›
Output:
0
💡 Note:
Target is already "000" and our string starts as "000", so no operations needed.
Example 3 — All Ones
$
Input:
target = "111"
›
Output:
1
💡 Note:
Start with "000". One flip at index 0 gives us "111", which matches the target.
Constraints
- 1 ≤ target.length ≤ 105
- target[i] is either '0' or '1'
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code