Show problems as
Classic problem #1758EasyString

Minimum Changes To Make Alternating Binary String

Real-world version: Flipping a Strip into Stripes
Tracked in this browser only
Read Minimum Changes To Make Alternating Binary String on LeetCode(opens in a new tab)

The 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.

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