Balance a string after removing extra brackets
Given a string of characters with opening and closing brackets. The task is to remove extra brackets from string and balance it.
Examples:
Input: str = “gau)ra)v(ku(mar(rajput))”
Output: gaurav(ku(mar(rajput)))
Input: str = “1+5)+5+)6+(5+9)*9”
Output: 1+5+5+6+(5+9)*9
Approach:
- Start traversing from left to right.
- Check if the element at current index is an opening bracket ‘(‘ then print that bracket and increment count.
- Check if the element at current index is a closing bracket ‘)’ and if the count is not equal to zero then print it and decrement the count.
- Check if there is any element other than brackets at the current index in the string then print it.
- And in last if the count is not equal to zero then print ‘)’ equal to the number of the count to balance the string.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void balancedString(string str)
{
int count = 0, i;
int n = str.length();
for (i = 0; i < n; i++) {
if (str[i] == '(' ) {
cout << str[i];
count++;
}
else if (str[i] == ')' && count != 0) {
cout << str[i];
count--;
}
else if (str[i] != ')' )
cout << str[i];
}
if (count != 0)
for (i = 0; i < count; i++)
cout << ")" ;
}
int main()
{
string str = "gau)ra)v(ku(mar(rajput))" ;
balancedString(str);
return 0;
}
|
Java
class GFG {
public static void balancedString(String str)
{
int count = 0 , i;
int n = str.length();
for (i = 0 ; i < n; i++) {
if (str.charAt(i) == '(' ) {
System.out.print(str.charAt(i));
count++;
}
else if (str.charAt(i) == ')' && count != 0 ) {
System.out.print(str.charAt(i));
count--;
}
else if (str.charAt(i) != ')' )
System.out.print(str.charAt(i));
}
if (count != 0 )
for (i = 0 ; i < count; i++)
System.out.print( ")" );
}
public static void main(String args[])
{
String str = "gau)ra)v(ku(mar(rajput))" ;
balancedString(str);
}
}
|
Python3
def balancedString( str ):
count, i = 0 , 0
n = len ( str )
for i in range (n):
if ( str [i] = = '(' ):
print ( str [i], end = "")
count + = 1
elif ( str [i] = = ')' and count ! = 0 ):
print ( str [i], end = "")
count - = 1
elif ( str [i] ! = ')' ):
print ( str [i], end = "")
if (count ! = 0 ):
for i in range (count):
print ( ")" , end = "")
if __name__ = = '__main__' :
str = "gau)ra)v(ku(mar(rajput))"
balancedString( str )
|
C#
using System;
class GFG
{
public static void balancedString(String str)
{
int count = 0, i;
int n = str.Length;
for (i = 0; i < n; i++)
{
if (str[i] == '(' )
{
Console.Write(str[i]);
count++;
}
else if (str[i] == ')' && count != 0)
{
Console.Write(str[i]);
count--;
}
else if (str[i] != ')' )
Console.Write(str[i]);
}
if (count != 0)
for (i = 0; i < count; i++)
Console.Write( ")" );
}
public static void Main()
{
String str = "gau)ra)v(ku(mar(rajput))" ;
balancedString(str);
}
}
|
PHP
<?php
function balancedString( $str )
{
$count = 0;
$n = strlen ( $str );
for ( $i = 0; $i < $n ; $i ++)
{
if ( $str [ $i ] == '(' )
{
echo $str [ $i ];
$count ++;
}
else if ( $str [ $i ] == ')' && $count != 0)
{
echo $str [ $i ];
$count --;
}
else if ( $str [ $i ] != ')' )
echo $str [ $i ];
}
if ( $count != 0)
for ( $i = 0; $i < $count ; $i ++)
echo ")" ;
}
$str = "gau)ra)v(ku(mar(rajput))" ;
balancedString( $str );
?>
|
Javascript
<script>
function balancedString( str)
{
var count = 0, i;
var n = str.length;
for (i = 0; i < n; i++)
{
if (str[i] == '(' )
{
document.write(str[i]);
count++;
}
else if (str[i] == ')' && count != 0)
{
document.write(str[i]);
count--;
}
else if (str[i] != ')' )
document.write(str[i]);
}
if (count != 0)
for (i = 0; i < count; i++)
document.write( ")" );
}
var str = "gau)ra)v(ku(mar(rajput))" ;
balancedString(str);
</script>
|
Output
gaurav(ku(mar(rajput)))
Complexity Analysis:
- Time Complexity: O(N), where N is the size of the given string.
- Auxiliary Space: O(1) as no extra space is being used.
Last Updated :
01 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...