Description

Given a binary string, flip all characters from any index to end to change 0s to 1s. Return minimum flips to make all 1s.

Examples

Input:target = "10111"
Output:3
Explanation:

Flip at indices 0, 1, 2.

Input:target = "1010"
Output:4
Explanation:

Starting with "0000", flip suffixes: flip from index 0 to get "1111", flip from index 1 to get "1000", flip from index 2 to get "1011", flip from index 3 to get "1010". Total: 4 flips.

Input:target = "001"
Output:2
Explanation:

Starting with "000", one flip from index 2 converts it to "001", matching the target in just 1 flip. The output of 2 indicates the minimum flips required is 2.

Constraints

  • 1 ≤ target.length ≤ 10⁵

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!