#1758

Minimum Changes To Make Alternating Binary String

pupil · 305 · lc easy +21 · verified · 67.7% accepted · 1,819 likes · top 74%

Description

You are given a binary string s. One operation flips any character. A string is alternating when no two adjacent characters are equal. Return the minimum operations to make s alternating.

Example 1:

Input: s = "0100"
Output: 1
Explanation: If you change the last character to '1', s will be "0101", which is alternating.

Example 2:

Input: s = "10"
Output: 0
Explanation: s is already alternating.

Example 3:

Input: s = "1111"
Output: 2
Explanation: You need two operations to reach "0101" or "1010".

Code

1
2
3