All problems
0884EasyString

Flipping a Strip into Stripes

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1758Minimum Changes To Make Alternating Binary String

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def min_operations(s: str) -> int:
Java
public int minOperations(String s)
September 7
Apply