#C11030. Minimum Operations to Make Binary String Alternating
Minimum Operations to Make Binary String Alternating
Minimum Operations to Make Binary String Alternating
You are given a binary string s
(i.e. a string consisting only of characters '0' and '1'). The task is to determine the minimum number of operations needed to convert the string into an alternating binary string. In one operation, you can flip a single character (i.e. change '0' to '1' or vice versa).
An alternating binary string is one that does not contain two consecutive similar characters. For example, 010101
and 101010
are alternating strings.
One approach is to compare the given string against two possible alternating patterns:
- $pattern_1 = 0101\ldots$
- $pattern_2 = 1010\ldots$
The answer is the minimum number of positions where the string does not match with either pattern. Mathematically, if we define the number of mismatches as:
$$mismatches = \min\{ \text{Hamming}(s, pattern_1),\ \text{Hamming}(s, pattern_2) \} $$Your task is to implement a function that computes this value.
inputFormat
The input consists of a single line containing a binary string s
.
outputFormat
Output a single integer which is the minimum number of operations required to make the binary string alternating.
## sample010101
0