Minimum Changes To Make Alternating Binary String

EasyStringGreedyMath

Description

Given a string s consisting only of 0s and 1s, return the minimum number of operations to make s alternating.

Examples

Input:s = "0100"
Output:1
Explanation:

The string '0100' needs one change at index 2 (changing '0' to '1') to become alternating '0101'. The alternative pattern '1010' would require 2 changes.

Input:s = "a"
Output:0
Explanation:

A single-character string is already alternating, so 0 changes are needed.

Input:s = "10110"
Output:2
Explanation:

Changing to "10101" requires 2 changes (positions 2 and 3), while changing to "01010" requires 3 changes (positions 0, 2, and 4). The minimum is 2.

Constraints

  • 1 ≤ s.length ≤ 10^4

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!