You are given a 0-indexed string
A string is called balanced if and only if:
- It is the empty string, or
-
It can be written as
AB , where bothA andB are balanced strings, or -
It can be written as
[C] , whereC 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
Output: 1
Explanation: You can string balanced by swapping index
Output: 2
Explanation: You can do the following swaps to make the string balanced:
- Swap index 0 with 4, and the resulting string will be "[]][][".
- Swap index 1 with index 5, and the resulting string will be "[[][]]".
Output: 0
Explanation: Input string is already balanced.
Constraints:
n == s.length 2 <= n <= 106 n is evens[i] is either'[' or']' -
The number of opening brackets
'[' equalsn / 2 , and the number of closing brackets']' equalsn / 2 .
Contents
Solution 1 - Using a stack Solution 2 - Keep track of opening brackets and closing brackets in a variable (Efficient solution)
In this approach, we are going to use a
-
We will create a
Stack to store opening brackets'[' , and another variableswaps to track how many swaps are required. -
Then, we will loop through characters of input string
s and do the following:-
If the character is an opening bracket
'[' , then add to the stack. -
If the character if a closing bracket
']' , then check if the stack is empty:-
If the stack is empty, that means a swap is needed, so we will increment the
swaps by1 . - If the stack is not empty, then we will take the top the element out from the stack to make opening bracket and closing brackets balanced.
-
If the stack is empty, that means a swap is needed, so we will increment the
-
If the character is an opening bracket
-
Finally, return the total swaps required:
(swaps + 1) /2 , below are the reasons why we are doing+1 anddivide by 2 .
- The division by 2 is because each swap operation addresses two unbalanced brackets. So, by counting the number of required swaps and then dividing that count by 2, it provides the actual number of unbalanced bracket pairs needing correction.
-
we are incrementing
swaps count by1 before divide by 2, to account for an edge case or a specific scenario where there might be an extra unbalanced bracket that requires attention.For instance, let's say the input string is
""]]]]][[[[[" , it will giveswaps = 5 . If we doswaps /2 , it implies that 2 swaps are required to balance 4 unbalanced bracket pairs. If you adjust it to(swaps + 1) / 2 , it would mean 3 unbalanced pairs plus one extra unbalanced bracket, needing 3 swaps in total.
Let's look at an example, say if the input string is
-
In the first step, we will initialize an empty
stack and a variableswaps , to keep track os how may swaps are needed. Then will loop through the characters of input strings . -
1st character in the input string is
] , so we will check whetherstack is empty, since it is empty we will incrementswaps to 1. -
2nd character in the input string is
] , so we will check whetherstack is empty, since it is empty we will incrementswaps to 2. -
3rd character in the input string is
] , so we will check whetherstack is empty, since it is empty we will incrementswaps to 3. -
4th character in the input string is
[ , so we will add it tostack .stack [ -
5th character in the input string is
[ , so we will add it tostack .stack [ [ -
6th character in the input string is
[ , so we will add it tostack .stack [ [ [ -
Now, we will return answer as
(3 + 1) /2 = 2 .
In this case input strings = "]]][[[" can be made to "[[][]]" with 2 swaps.
=> Swap 0th index element] with 4th index element[ , aftwr the swap it looks like this[]][][
=> Swap 1st index element] with 5th index element[ , aftwr the swap it looks like this[[][]]
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(n) since we are storing opening brackets
This is similar to above solution, instead of storing the opening brackets in a stack, we will maintain them in a variable
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(1) since we are only two variables
Above implementations source code can be found at