Check if it is possible to obtain a Balanced Parenthesis by shifting brackets to either end at most K times
Last Updated :
25 May, 2021
Given a string S of size N consisting of only ‘(‘ and ‘)’ only and a positive integer K, the task is to check if the given string can be made a valid parenthesis sequence by moving any characters of the string S to either end of the string at most K number of times.
Examples:
Input: S = “)(“, K = 1
Output: Yes
Explanation: Move S[0] to the end of the string.
Now, the modified string S is “()” which is balanced. Therefore, the number of moves required is 1( = K).
Input: S = “()()”, K = 0
Output: Yes
Approach: The given problem can be solved based on the following observations:
- If N is odd or the count of opening and closing brackets are not equal, then it is not possible to make a valid parenthesis sequence.
- The idea is to traverse the given sequence and keep track of the difference of count of opening and closing brackets, and if the difference becomes negative at any index, then move some opening bracket after the current index and move it to the beginning.
Follow the steps below to solve the problem:
- If N is odd or the count of opening and closing brackets are not equal, then it is not possible to make a valid parenthesis sequence. Hence, print “No”. Otherwise, perform the following steps:
- Initialize two variables, say count and ans as 0 that keeps track of the difference of opening and closing brackets and the required number of moves respectively.
- Traverse the given string S and perform the following steps:
- If the current character S[i] is ‘(‘, then increment the value of count by 1.
- Otherwise, decrement the value of count by 1.
- If the count is less than 0, then update the count to 0, and increment the value of ans by 1.
- After completing the above steps, if the value of ans is at most K, then print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minimumMoves(string s, int n, int k)
{
if (n & 1) {
cout << "No" ;
return ;
}
int countOpen = count(s.begin(),
s.end(), '(' );
int countClose = count(s.begin(),
s.end(), ')' );
if (countOpen != countClose) {
cout << "No" ;
return ;
}
int ans = 0;
int cnt = 0;
for ( int i = 0; i < n; ++i) {
if (s[i] == '(' )
++cnt;
else {
--cnt;
if (cnt < 0) {
cnt = 0;
++ans;
}
}
}
if (ans <= k)
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
string S = ")(" ;
int K = 1;
minimumMoves(S, S.length(), K);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static void minimumMoves(String s, int n, int k)
{
if (n % 2 == 1 )
{
System.out.println( "No" );
return ;
}
int countOpen = 0 , countClose = 0 ;
for ( char ch : s.toCharArray())
if (ch == '(' )
countOpen++;
else if (ch == ')' )
countClose++;
if (countOpen != countClose)
{
System.out.println( "No" );
return ;
}
int ans = 0 ;
int cnt = 0 ;
for ( int i = 0 ; i < n; ++i)
{
if (s.charAt(i) == '(' )
++cnt;
else
{
--cnt;
if (cnt < 0 )
{
cnt = 0 ;
++ans;
}
}
}
if (ans <= k)
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main(String[] args)
{
String S = ")(" ;
int K = 1 ;
minimumMoves(S, S.length(), K);
}
}
|
Python3
def minimumMoves(s, n, k):
if (n & 1 ):
print ( "No" )
return
countOpen = s.count( '(' )
countClose = s.count( ')' )
if (countOpen ! = countClose):
print ( "No" )
return
ans = 0
cnt = 0
for i in range (n):
if (s[i] = = '(' ):
cnt + = 1
else :
cnt - = 1
if (cnt < 0 ):
cnt = 0
ans + = 1
if (ans < = k):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = "__main__" :
S = ")("
K = 1
minimumMoves(S, len (S), K)
|
C#
using System;
class GFG{
static void minimumMoves( string s, int n, int k)
{
if (n % 2 == 1)
{
Console.WriteLine( "No" );
return ;
}
int countOpen = 0, countClose = 0;
foreach ( char ch in s.ToCharArray())
if (ch == '(' )
countOpen++;
else if (ch == ')' )
countClose++;
if (countOpen != countClose)
{
Console.WriteLine( "No" );
return ;
}
int ans = 0;
int cnt = 0;
for ( int i = 0; i < n; ++i)
{
if (s[i] == '(' )
++cnt;
else
{
--cnt;
if (cnt < 0)
{
cnt = 0;
++ans;
}
}
}
if (ans <= k)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
static void Main()
{
string S = ")(" ;
int K = 1;
minimumMoves(S, S.Length, K);
}
}
|
Javascript
<script>
function minimumMoves(s,n,k)
{
if (n & 1) {
document.write( "No" );
return ;
}
var countOpen = 0;
var i;
for (i=0;i<s.length;i++){
if (s[i]== "(" )
countOpen++;
}
var countClose = 0;
for (i=0;i<s.length;i++){
if (s[i]== ")" )
countClose++;
};
if (countOpen != countClose) {
document.write( "No" );
return ;
}
var ans = 0;
var cnt = 0;
for (i = 0; i < n; ++i) {
if (s[i] == '(' )
++cnt;
else {
--cnt;
if (cnt < 0) {
cnt = 0;
++ans;
}
}
}
if (ans <= k)
document.write( "Yes" );
else
document.write( "No" );
}
var S = ")(" ;
var K = 1;
minimumMoves(S, S.length, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...