Minimum Number of Swaps to Make the String Balanced - Complete Solution Guide
Minimum Number of Swaps to Make the String Balanced is LeetCode problem 1963, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given a 0-indexed string s of even length n . The string consists of exactly n / 2 opening brackets '[' and n / 2 closing brackets ']' . A string is called balanced if and only 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. You may swap the brackets at any two indices any number of times. Return the minimum number of swaps to make s balanced . Example 1: Input: s = "][][" Output
Detailed Explanation
The problem requires finding the minimum number of swaps needed to balance a string `s` containing an equal number of opening brackets '[' and closing brackets ']'. A balanced string can be empty, formed by concatenating two balanced strings, or enclosed within square brackets where the enclosed string is balanced. The string initially has mismatched brackets and needs to be balanced using swaps between any two indices.
Solution Approach
The solution iterates through the string, maintaining a `balance` counter. This counter is decremented when an opening bracket '[' is encountered and incremented when a closing bracket ']' is encountered. `max_balance` keeps track of the maximum value the `balance` counter reaches during the iteration. The final answer is derived from `max_balance`. The intuition is that `max_balance` indicates the maximum number of extra closing brackets encountered before their corresponding opening brackets. We need to swap some closing brackets with opening brackets that appear later in the string to correct the imbalance. The formula (max_balance + 1) // 2 calculates the minimum number of swaps required to achieve balance.
Step-by-Step Algorithm
- Step 1: Initialize `balance` and `max_balance` to 0.
- Step 2: Iterate through the string `s` from left to right.
- Step 3: If the current character is '[', decrement the `balance` counter.
- Step 4: If the current character is ']', increment the `balance` counter.
- Step 5: Update `max_balance` to be the maximum of its current value and the current `balance`.
- Step 6: After the loop finishes, return `(max_balance + 1) // 2`.
- Step 7: Integer division is used to determine the minimum number of swaps needed.
Key Insights
- Insight 1: The number of opening and closing brackets are equal, meaning a balanced state exists.
- Insight 2: The problem can be solved by tracking the imbalance between opening and closing brackets as we iterate through the string.
- Insight 3: The maximum imbalance encountered during the iteration indicates the minimum number of swaps needed.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Two Pointers, String, Stack, Greedy.
Companies
Asked at: Expedia, Microstrategy, Nutanix, PayPal, Twilio.