← Back to challenges

Minimum Swaps to Alternate a Binary String

JavaScriptHardstringsloops

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

minSwaps("101010") ➞ 0

minSwaps("10001110") ➞ 1
// Swap the 0 at index 2 for the 1 at index 5.

minSwaps("11110000") ➞ 2

Notes

N/A

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