← Back to challenges

Minimum Swaps to Alternate a Binary String

PythonHardstringsloops

Instructions

Write a function that returns the minimum number of swaps to create an alternating binary string. The input will have the same number of zeroes and ones.

Examples

min_swaps("101010") ➞ 0

min_swaps("10001110") ➞ 1
# Swap the 0 at index 2 for the 1 at index 5.

min_swaps("11110000") ➞ 2

Notes

N/A

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.