Cost to Balance the parentheses
Parentheses are said to be balanced when every opening brace has a closing brace like “()()” or “(())” or “(()())” etc. Incorrect balancing includes “)(” or “))((” etc. The task here is to correct the sequence of parentheses in such a way that it is done in minimum cost. And shifting of parentheses by over one parentheses costs 1. If the parentheses can’t be balanced then print -1.
Examples :
Input : ()
Output : 0
Explanation : Already balanced
Input : ))((
Output : 4
Explanation : Firstly, ) at position 1st goes to the last position, costing 3, so we get )((). Then, ) at position 1st goes to the 2nd position costing 1. So, finally we get ()(). Therefore, the total cost is 4.
Algorithm :
- Store the braces in string.
- Run a loop to string size to store the count of opening and closing braces.
- Check if number of opening brace is equal to number of closing brace or not.
- If the braces are not equal then print -1 depicting that the string cant be balanced. Else proceed further.
- Initially, Check at 0th index that whether the string contains opening brace or closing brace. If we get an opening brace then store +1 in the array at index 0, else if closing brace is present then place -1 at 0th index.
- Now run a loop from 1st index to array length.
- If opening brace is present at index i then add +1 to value at previous index i.e. i-1 and store sum at index i.
- If closing brace is present at index i then add -1 to value at previous index i.e. i-1 and store sum at index i.
- If value at index i is negative i.e less than 0, then add the absolute value of array[i] into a variable(ans in below program).
- Finally we get the minimum cost in variable ans.
Below is the implementation of above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int costToBalance(string s)
{
if (s.length() == 0)
cout << 0 << endl;
int ans = 0;
int o = 0, c = 0;
for ( int i = 0; i < s.length(); i++) {
if (s[i] == '(' )
o++;
if (s[i] == ')' )
c++;
}
if (o != c)
return -1;
int a[s.size()];
if (s[0] == '(' )
a[0] = 1;
else
a[0] = -1;
if (a[0] < 0)
ans += abs (a[0]);
for ( int i = 1; i < s.length(); i++) {
if (s[i] == '(' )
a[i] = a[i - 1] + 1;
else
a[i] = a[i - 1] - 1;
if (a[i] < 0)
ans += abs (a[i]);
}
return ans;
}
int main()
{
string s;
s = ")))(((" ;
cout << costToBalance(s) << endl;
s = "))((" ;
cout << costToBalance(s) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int costToBalance(String s)
{
if (s.length() == 0 )
System.out.println( 0 );
int ans = 0 ;
int o = 0 , c = 0 ;
for ( int i = 0 ; i < s.length(); i++)
{
if (s.charAt(i) == '(' )
o++;
if (s.charAt(i) == ')' )
c++;
}
if (o != c)
return - 1 ;
int []a = new int [s.length()];
if (s.charAt( 0 ) == '(' )
a[ 0 ] = 1 ;
else
a[ 0 ] = - 1 ;
if (a[ 0 ] < 0 )
ans += Math.abs(a[ 0 ]);
for ( int i = 1 ; i < s.length(); i++)
{
if (s.charAt(i) == '(' )
a[i] = a[i - 1 ] + 1 ;
else
a[i] = a[i - 1 ] - 1 ;
if (a[i] < 0 )
ans += Math.abs(a[i]);
}
return ans;
}
public static void main(String args[])
{
String s;
s = ")))(((" ;
System.out.println(costToBalance(s));
s = "))((" ;
System.out.println(costToBalance(s));
}
}
|
Python3
def costToBalance(s):
if ( len (s) = = 0 ):
print ( 0 )
ans = 0
o = 0
c = 0
for i in range ( len (s)):
if (s[i] = = '(' ):
o + = 1
if (s[i] = = ')' ):
c + = 1
if (o ! = c):
return - 1
a = [ 0 for i in range ( len (s))]
if (s[ 0 ] = = '(' ):
a[ 0 ] = 1
else :
a[ 0 ] = - 1
if (a[ 0 ] < 0 ):
ans + = abs (a[ 0 ])
for i in range ( 1 , len (s)):
if (s[i] = = '(' ):
a[i] = a[i - 1 ] + 1
else :
a[i] = a[i - 1 ] - 1
if (a[i] < 0 ):
ans + = abs (a[i])
return ans
if __name__ = = '__main__' :
s = ")))((("
print (costToBalance(s))
s = "))(("
print (costToBalance(s))
|
C#
using System;
class GFG
{
static int costToBalance( string s)
{
if (s.Length == 0)
Console.WriteLine(0);
int ans = 0;
int o = 0, c = 0;
for ( int i = 0; i < s.Length; i++)
{
if (s[i] == '(' )
o++;
if (s[i] == ')' )
c++;
}
if (o != c)
return -1;
int []a = new int [s.Length];
if (s[0] == '(' )
a[0] = 1;
else
a[0] = -1;
if (a[0] < 0)
ans += Math.Abs(a[0]);
for ( int i = 1; i < s.Length; i++)
{
if (s[i] == '(' )
a[i] = a[i - 1] + 1;
else
a[i] = a[i - 1] - 1;
if (a[i] < 0)
ans += Math.Abs(a[i]);
}
return ans;
}
static void Main()
{
string s;
s = ")))(((" ;
Console.WriteLine (costToBalance(s));
s = "))((" ;
Console.WriteLine (costToBalance(s));
}
}
|
Javascript
<script>
function costToBalance( s)
{
if (s.length == 0)
document.write(0);
var ans = 0;
var o = 0, c = 0;
for ( var i = 0; i < s.length; i++)
{
if (s[i] == '(' )
o++;
if (s[i] == ')' )
c++;
}
if (o != c)
return -1;
var a = new Array(s.Length);
if (s[0] == '(' )
a[0] = 1;
else
a[0] = -1;
if (a[0] < 0)
ans += Math.abs(a[0]);
for ( var i = 1; i < s.length; i++)
{
if (s[i] == '(' )
a[i] = a[i - 1] + 1;
else
a[i] = a[i - 1] - 1;
if (a[i] < 0)
ans += Math.abs(a[i]);
}
return ans;
}
var s;
s = ")))(((" ;
document.write(costToBalance(s) + "<br>" );
s = "))((" ;
document.write(costToBalance(s) + "<br>" );
</script>
|
Complexity Analysis:
- Time Complexity: O(N),N=String Length
- Auxiliary Space: O(N)
Last Updated :
26 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...