#926

Flip String to Monotone Increasing

specialist · 680 · lc medium +30 · verified · 61.9% accepted · 4,583 likes · top 62%

Description

A binary string is monotone increasing if all its '0's appear before any '1's. Given a binary string s, find the minimum number of character flips (changing '0' to '1' or vice versa) required to make s monotone increasing.

Example 1:

Input: s = "00110"
Output: 1
Explanation: We flip the last digit to get 00111.

Example 2:

Input: s = "010110"
Output: 2
Explanation: We flip to get 011111, or alternatively 000111.

Example 3:

Input: s = "00011000"
Output: 2
Explanation: We flip to get 00000000.

Code

1
2
3