Check if the bracket sequence can be balanced with at most one change in the position of a bracket
Last Updated :
13 Jul, 2022
Given an unbalanced bracket sequence as a string str, the task is to find whether the given string can be balanced by moving at most one bracket from its original place in the sequence to any other position.
Examples:
Input: str = “)(()”
Output: Yes
As by moving s[0] to the end will make it valid.
“(())”
Input: str = “()))(()”
Output: No
Approach: Consider X as a valid bracket then definitely (X) is also valid. If X is not valid and can be balanced with just one change of position in some bracket then it must be of type X = “)(“ where ‘)’ has been placed before ‘(‘.
Now, X can be replaced with (X) as it will not affect the balanced nature of X. The new string becomes X = “()()” which is balanced.
Hence, if (X) is balanced then we can say that X can be balanced with at most one change in the position of some bracket.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool canBeBalanced(string s, int n)
{
if (n % 2 == 1)
return false ;
string k = "(" ;
k += s + ")" ;
vector<string> d;
int cnt = 0;
for ( int i = 0; i < k.length(); i++)
{
if (k[i] == '(' )
d.push_back( "(" );
else
{
if (d.size() != 0)
d.pop_back();
else
return false ;
}
}
if (d.empty())
return true ;
return false ;
}
int main( int argc, char const *argv[])
{
string s = ")(()" ;
int n = s.length();
(canBeBalanced(s, n)) ? cout << "Yes"
<< endl : cout << "No" << endl;
return 0;
}
|
Java
import java.util.Vector;
class GFG
{
static boolean canBeBalanced(String s, int n)
{
if (n % 2 == 1 )
return false ;
String k = "(" ;
k += s + ")" ;
Vector<String> d = new Vector<>();
for ( int i = 0 ; i < k.length(); i++)
{
if (k.charAt(i) == '(' )
d.add( "(" );
else
{
if (d.size() != 0 )
d.remove(d.size() - 1 );
else
return false ;
}
}
if (d.isEmpty())
return true ;
return false ;
}
public static void main(String[] args)
{
String s = ")(()" ;
int n = s.length();
if (canBeBalanced(s, n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def canBeBalanced(s, n):
if n % 2 = = 1 :
return False
k = "("
k = k + s + ")"
d = []
count = 0
for i in range ( len (k)):
if k[i] = = "(" :
d.append( "(" )
else :
if len (d)! = 0 :
d.pop()
else :
return False
if len (d) = = 0 :
return True
return False
S = ")(()"
n = len (S)
if (canBeBalanced(S, n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static bool canBeBalanced( string s, int n)
{
if (n % 2 == 1)
return false ;
string k = "(" ;
k += s + ")" ;
List< string > d = new List< string >();
for ( int i = 0; i < k.Length; i++)
{
if (k[i] == '(' )
d.Add( "(" );
else
{
if (d.Count != 0)
d.RemoveAt(d.Count - 1);
else
return false ;
}
}
if (d.Count == 0)
return true ;
return false ;
}
public static void Main()
{
string s = ")(()" ;
int n = s.Length;
if (canBeBalanced(s, n))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function canBeBalanced(s,n)
{
if (n % 2 == 1)
return false ;
let k = "(" ;
k += s + ")" ;
let d = [];
for (let i = 0; i < k.length; i++)
{
if (k[i] == '(' )
d.push( "(" );
else
{
if (d.length != 0)
d.pop();
else
return false ;
}
}
if (d.length==0)
return true ;
return false ;
}
let s = ")(()" ;
let n = s.length;
if (canBeBalanced(s, n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity : O(n) ,where n is size of given string
Space Complexity : O(n)
Share your thoughts in the comments
Please Login to comment...