Skip to main content
Back to problems
Leetcode
Easy
Arrays
Strings
Greedy
Minimum Changes to Make Alternating Binary String

Given a binary string, find the minimum number of character changes needed to make it alternating.

Acceptance 100%
Problem Statement

Problem

You are given a binary string s consisting only of '0' and '1'.

In one move, you may change any character in the string to the other binary digit.

A string is alternating if no two adjacent characters are the same. For example, 0101 and 1010 are alternating, while 0110 is not.

Return the minimum number of moves required to make s alternating.

Key Idea

For an alternating string, there are only two possible target patterns:

  • starts with 0: 010101...
  • starts with 1: 101010...

Compute how many positions differ from each pattern and return the smaller count.

Input Format

A single binary string s.

  • 1 <= s.length

Output Format

Return an integer representing the minimum number of changes required so that the string becomes alternating.

Constraints

  • s contains only characters '0' and '1'.
  • The answer is guaranteed to fit in a 32-bit signed integer.
Examples
Sample cases returned by the problem API.

Example 1

Input

s = "0100"

Output

1

Explanation

Change the last character from '0' to '1' to get "0101".

Example 2

Input

s = "10"

Output

0

Explanation

The string is already alternating.

Show 1 more example

Example 3

Input

s = "1111"

Output

2

Explanation

Both target patterns require two changes: "1010" or "0101".

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.