brackets of even length n with exactly n / 2 opening brackets [ and exactly n / 2 closing brackets ].
A bracket string is balanced when every opening bracket can be paired with a closing bracket to its right so that pairs never cross, as in [[]] or [][] but not in ][ or []][.
In one move you may swap any two characters of the string. Return the fewest moves that make brackets balanced.brackets="]][[" → 1
Swap the characters at indices 0 and 3 to get [][].
brackets="[][]" → 0
The string is already balanced.
brackets="]]]][[[[" → 2
Swapping indices 0 and 7 gives []]][[[], then swapping indices 2 and 5 gives [][][][]. One swap is not enough.
brackets consists only of [ and ]brackets="]][["
1
Run checks the first 3 tests in your browser. Submit checks all 34 tests on the server, hidden ones included.