Minimum Number of Swaps to Make the String Balanced - Problem

๐ŸŽฏ Minimum Number of Swaps to Make the String Balanced

You are given a string s consisting of exactly n/2 opening brackets '[' and n/2 closing brackets ']', where n is the even length of the string.

A string is considered balanced if:

  • It is the empty string, or
  • It can be written as AB, where both A and B are balanced strings, or
  • It can be written as [C], where C is a balanced string.

Example: "[[]]" is balanced, but "]]][" is not balanced.

You can swap any two brackets at any positions any number of times. Your goal is to find the minimum number of swaps needed to make the string balanced.

Key Insight: Since we have equal numbers of opening and closing brackets, we only need to fix the misplaced closing brackets that appear before their matching opening brackets.

Input & Output

example_1.py โ€” Basic unbalanced string
$ Input: s = "][]["
โ€บ Output: 1
๐Ÿ’ก Note: You can make the string balanced by swapping index 0 with index 1 to get "[]", or swap index 0 with index 3 to get "[]]][[" then swap index 1 with index 2 to get "[[]]".
example_2.py โ€” Already balanced
$ Input: s = "[]"
โ€บ Output: 0
๐Ÿ’ก Note: The string is already balanced, so no swaps are needed.
example_3.py โ€” Complex case
$ Input: s = "]]]][[[["
โ€บ Output: 2
๐Ÿ’ก Note: We have 4 unmatched closing brackets at the beginning. We can fix 2 unmatched closing brackets with 1 swap, so we need 2 swaps total: ceil(4/2) = 2.

Constraints

  • n == s.length
  • 2 โ‰ค n โ‰ค 106
  • n is even
  • s[i] is either '[' or ']'
  • The number of opening brackets '[' equals the number of closing brackets ']'

Visualization

Tap to expand
Balance Tracking for ]]][]pos 0balance: -1debt: 1]pos 1balance: -1debt: 2]pos 2balance: -1debt: 3[pos 3balance: 1debt: 3Final Calculation:Total debt (unmatched closing brackets): 3Each swap fixes 2 unmatched bracketsMinimum swaps = ceil(3 / 2) = 2Swap Visualization:]]][ โ†’ swap pos 0,3 โ†’ []][[]][ โ†’ swap pos 1,2 โ†’ [[]]โœ… Balanced in 2 swaps!๐Ÿ’ก Key Insight: We only need to count unmatched closing brackets.Each swap converts one ']' to '[' and one '[' to ']', fixing 2 unmatched brackets.
Understanding the Visualization
1
Initialize Balance
Start with balance = 0, like an empty bank account
2
Process Brackets
Opening brackets add 1, closing brackets subtract 1 from balance
3
Track Debt
When balance goes negative, count the 'debt' and reset balance to 0
4
Calculate Swaps
Each swap fixes 2 units of debt, so swaps = ceil(debt / 2)
Key Takeaway
๐ŸŽฏ Key Insight: Track the running balance of brackets - when it goes negative, we have unmatched closing brackets that need swapping. Each swap fixes exactly 2 unmatched brackets.
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
52.8K Views
High Frequency
~15 min Avg. Time
2.1K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen