Back to DSA sheet
Minimum Number of Swaps to Make The String Balanced
MediumArraysA string of even length contains equal numbers of '[' and ']'. In one move you may swap any two characters. Return the minimum number of swaps to make the string balanced (a valid bracket sequence).
Input format (stdin): a single line, the bracket string. Output the minimum swaps.
Examples
Input: ][][
Output: 1
Swap to get [][].
Input: ]]][[[
Output: 2
Two swaps suffice.
Constraints
- 2 <= length <= 10^6
- equal counts of '[' and ']'
Sheets
NeetCode 250
minimum-number-of-swaps-to-make-the-string-balanced.cpp3 sample tests
Loading editor
Test results
Run the sample tests to check your solution against expected output.
Custom input (stdin)
Output
Run your code to see its output.