Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2
Last Updated :
15 Feb, 2023
Given a 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: The problem can be solved using a stack as discussed in this post. In this article, an approach that doesn’t use extra space will be discussed.
If the frequency of ‘(‘ is less than frequency of ‘)’. If the above difference is greater than 1 then the sequence cannot be balanced else it can be balanced if the overall difference is zero.
Below is implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool canBeBalanced(string s, int n)
{
int count = 0, count_1 = 0;
for ( int i = 0; i < n; i++) {
if (s[i] == '(' )
count++;
else
count--;
count_1 = min(count_1, count);
}
if (count_1 >= -1 && count == 0)
return true ;
return false ;
}
int main()
{
string s = "())()(" ;
int n = s.length();
if (canBeBalanced(s, n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG
{
static boolean canBeBalanced(String s, int n)
{
int count = 0 , count_1 = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (s.charAt(i) == '(' )
count++;
else
count--;
count_1 = Math.min(count_1, count);
}
if (count_1 >= - 1 && count == 0 )
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):
count = 0
count_1 = 0
for i in range (n):
if (s[i] = = '(' ):
count + = 1
else :
count - = 1
count_1 = min (count_1, count)
if (count_1 > = - 1 and count = = 0 ):
return True
return False
s = "())()("
n = len (s)
if (canBeBalanced(s, n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static Boolean canBeBalanced(String s, int n)
{
int count = 0, count_1 = 0;
for ( int i = 0; i < n; i++)
{
if (s[i] == '(' )
count++;
else
count--;
count_1 = Math.Min(count_1, count);
}
if (count_1 >= -1 && count == 0)
return true ;
return false ;
}
public static void Main(String []args)
{
String s = "())()(" ;
int n = s.Length;
if (canBeBalanced(s, n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function canBeBalanced( s, n)
{
var count = 0, count_1 = 0;
for ( var i=0; i < n; i++) {
if (s[i] == '(' )
count++;
else
count--;
count_1 = Math.min(count_1, count);
}
if (count_1 >= -1 && count == 0)
return true ;
return false ;
}
var s = "())()(" ;
var n = s.length;
if (canBeBalanced(s, n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(n), where n is the length of string s.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...