A settings page shows a row of toggles as a string of 0 (off) and 1 (on). The design team wants every off toggle to come before every on toggle, like 000111, so they flip some toggles. A row of all off or all on toggles also counts.
Write fewestFlips(toggles) that returns the fewest toggles to flip.
Input: toggles = "010110" Output: 2
Flip positions 1 and 5 to get 000111.
Input: toggles = "111" Output: 0
All on already counts.
1 <= toggles.length <= 100000toggles contains only 0 and 1.Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #556 Fewest Flips to Sort a Toggle Row. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.