#C3060. Minimum Swaps to Make Binary String Alternating
Minimum Swaps to Make Binary String Alternating
Minimum Swaps to Make Binary String Alternating
You are given a binary string of length \(n\) where \(1\le n\le 10^5\) and each character is either '0' or '1'. Your task is to determine the minimum number of swaps needed to transform the string into an alternating string. A string is called alternating if no two adjacent characters are the same, for example, "010101" or "101010".
In one swap operation, you can choose any two characters in the string and swap their positions. If it is not possible to convert the given string into an alternating one, output \(-1\).
Note: When counting swaps, consider that swapping two mismatched positions will correct both positions. Thus, the number of required swaps is half of the total mismatches for a valid candidate alternating pattern.
inputFormat
The input is provided via stdin and consists of a single line containing a binary string.
Example:
1001
outputFormat
Output a single integer via stdout representing the minimum number of swaps required to turn the binary string into an alternating string, or \(-1\) if it is not possible.
Example:
1## sample
101010
0