Minimum Swaps to Alternate a Binary String

Published by Helen Yu in

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

Watch a quick demo on how Edabit works.