Minimum Flips for Even Binary Runs
Problem statement
You are given an even-length binary string password. In one flip, change a single 0 to 1 or a single 1 to 0.
After the flips, the whole string must be split into non-overlapping substrings such that:
- Every substring has even length.
- Every substring contains only one kind of character: all
0s or all1s.
Return the minimum number of flips needed. You may choose any number of substrings, including one.
Function
getMinFlips(password: String) → intExamples
Example 1
password = "1110011000"return = 3Using zero-based positions, flip the zeros at positions 3, 4, and 7. The string becomes 1111111100. Split it into eight 1s followed by two 0s; both parts have even length and one character type.
The original consecutive pairs are 11, 10, 01, 10, and 00. Three pairs differ, and each requires at least one flip, so three flips are optimal.
Example 2
password = "001111"return = 0The string already splits into 00 and 1111, of lengths two and four. Both parts are uniform and have even length, so no flips are needed.
Constraints
2 <= password.length <= 10^5- The length of
passwordis even. passwordcontains only0and1.