Minimum swaps to balance the given brackets at any index
Last Updated :
12 Oct, 2021
Given a balanced string of even length consisting of equal number of opening brackets ‘[‘ and closing brackets ‘]’ , Calculate the minimum number of swaps to make string balanced. An unbalanced string can be made balanced by swapping any two brackets.
A string is called balanced if it can be represented in the form of “[S1]” where S1 is a balanced string
Example:
Input: s= “] ] ] [ [ [“
Output: 2
Explanation: First swap: Position 0 and 5 = [ ] ] [ [ ]
Second swap: Position 2 and 3 = [][][]
Input: s= “] ] ] ] [ ] [ [ [ [“
Output: 2
Explanation: first swap for brackets at position 2 and 5, second swap for brackets at position 0 and 7
Approach: Given problem can be solved by iterating through the string and following the steps below:
- All the balanced brackets are removed as they do not require any swaps for balancing the string
- Since, the number of opening bracket ‘[‘ and closing bracket is same ‘]’, After removing balanced components , remaining string becomes like ] ] ]…..[ [
- The optimal approach is to balance two sets of brackets in one swap
- For every two pairs of square brackets, a swap will make them balanced.
- If number of unbalanced pairs are odd, then one more swap is needed.
- If p is the number of unbalanced pairs then
minimum number of swaps = (p + 1) / 2
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
int BalancedStringBySwapping(string s)
{
int unbalancedPair = 0;
for ( int i = 0; i < s.length(); ++i)
{
if (unbalancedPair > 0 && s[i] == ']' )
{
--unbalancedPair;
}
else if (s[i] == '[' )
{
++unbalancedPair;
}
}
return (unbalancedPair + 1) / 2;
}
int main()
{
string s = "]]][[[" ;
cout << (BalancedStringBySwapping(s));
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int BalancedStringBySwapping(String s)
{
int unbalancedPair = 0 ;
for ( int i = 0 ; i < s.length(); ++i) {
if (unbalancedPair > 0 && s.charAt(i) == ']' ) {
--unbalancedPair;
}
else if (s.charAt(i) == '[' ) {
++unbalancedPair;
}
}
return (unbalancedPair + 1 ) / 2 ;
}
public static void main(String[] args)
{
String s = "]]][[[" ;
System.out.println(BalancedStringBySwapping(s));
}
}
|
Python3
def BalancedStringBySwapping(s) :
unbalancedPair = 0 ;
for i in range ( len (s)) :
if (unbalancedPair > 0 and s[i] = = ']' ) :
unbalancedPair - = 1 ;
elif (s[i] = = '[' ) :
unbalancedPair + = 1 ;
return (unbalancedPair + 1 ) / / 2 ;
if __name__ = = "__main__" :
s = "]]][[[" ;
print (BalancedStringBySwapping(s));
|
C#
using System;
class GFG
{
static int BalancedStringBySwapping(String s)
{
int unbalancedPair = 0;
for ( int i = 0; i < s.Length; ++i) {
if (unbalancedPair > 0 && s[i] == ']' ) {
--unbalancedPair;
}
else if (s[i] == '[' ) {
++unbalancedPair;
}
}
return (unbalancedPair + 1) / 2;
}
public static void Main(String[] args)
{
String s = "]]][[[" ;
Console.Write(BalancedStringBySwapping(s));
}
}
|
Javascript
<script>
function BalancedStringBySwapping(s)
{
var unbalancedPair = 0;
for ( var i = 0; i < s.length; ++i)
{
if (unbalancedPair > 0 && s[i] == ']' )
{
--unbalancedPair;
}
else if (s[i] == '[' )
{
++unbalancedPair;
}
}
return (unbalancedPair + 1) / 2;
}
var s = "]]][[[" ;
document.write(BalancedStringBySwapping(s));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...