Minimum Changes To Make Alternating Binary String
Real-world version: Flipping a Strip into StripesThe statement here is our own: the same inputs, outputs and technique.
A strip of cells reads s, a string of '0' and '1'. One flip changes a single cell to the other character.
The strip is striped when no two neighbouring cells hold the same character. Return the fewest flips that leave the strip striped.
Examples
Example 1
- Input
- s = "1100110011"
- Output
- 5
Aiming for the strip that starts with a one leaves five cells wrong, and aiming for the other leaves five as well, so five flips is the best either way.
Example 2
- Input
- s = "111000111"
- Output
- 3
Against the version starting with a one, the cells at positions 1, 3, 5, 7 are wrong, which is four flips. The other version needs five.
Example 3
- Input
- s = "010101010101"
- Output
- 0
The strip already alternates, so nothing needs flipping.
Constraints
- 1 <= s.length <= 10^4
- s[i] is either '0' or '1'
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def min_operations(s: str) -> int:public int minOperations(String s)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 11 test cases: 11 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's