


Ninja is playing with a string ‘S’ consisting of only equal numbers of opening brackets ‘[’ and closing brackets ’]’. The string was initially balanced but due to Ninja’s friend’s mischief, the string got shuffled up. Now, Ninja wants to make the string balanced again using the minimum number of swaps. Can you help Ninja to find the minimum number of swaps required to make the string balanced again?
A string is called balanced if and only if:
1.It is the empty string, or 2.It can be written as AB, where both A and B are balanced strings, or 3.It can be written as [C], where C is a balanced string.You are given a string ‘S’ having ‘N/2’ opening brackets and N/2 closing brackets. You have to find the minimum number of swaps required to make ‘S’ balanced again.
For ExampleIf the given string S = “ [[]]][ ”, the answer will be 1 as if we swap the last and second last character, the string will be balanced.
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains two integers, ‘N’ denoting the number of characters present in string S.
The following line contains the string ‘S’.
Output Format:
For each test case, print ‘an integer corresponding to the minimum number of swaps required.
Print the output of each test case in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^6.
S[i] = { ‘[’ , ‘]’}.
Time limit: 1 sec
2
6
[[]]][
6
]]][[[
1
2
For the first test case,
The minimum number of swaps required is 1.
Swap 1: Swap S[5] and S[4].
The resultant string will be “[[[]]]” which is balanced. Hence, the answer is 1.
For the second test case:
The minimum number of swaps required is 2.
Swap 1: Swap S[0] and S[5].
Swap 2: Swap S[2] and S[3]
The resultant string will be “[][][]” which is balanced. Hence, the answer is 2.
2
[]
6
][]][[
0
1
Check for the character which should be swapped and select a suitable character with whom that will be swapped.
In this approach, we will declare two pointers ‘i’ and ‘j’ to traverse the string. ‘i‘ will be pointing to the first character and ‘j’ will be pointing to the last character. We will store the number of unused opening brackets in variable ‘C’. We will iterate ‘i’ from left to right, if we find any opening bracket, we will increment ‘C’ to ‘C+1’. If we found a closing bracket, we will decrement ‘C’ to ‘C’-1 as one opening bracket will be used. If ‘C’ became -1, that implies the closing bracket should be swapped with an opening bracket. So, they will find the first opening bracket from the end using pointer ‘j’ and swap them and increment ‘ANS’ as ‘ANS+1’.
At last, we will return ‘ANS’ storing the minimum number of swaps required.
Algorithm:
O(N), where ‘N’ is the number of characters in string ‘S’.
In this approach, we traverse the whole string using two pointers, so the number of operations will be O(N). Hence, the overall time complexity is O(N).
O(1).
In this approach, we are using constant space. Hence, the overall space complexity is O(1).