Given a string S of length N, consisting of only opening ‘(‘ and closing ‘)‘ parenthesis. The task is to find all indices ‘K‘ such that S[K…N-1] + S[0…K-1] is a regular parenthesis.
A regular parentheses string is either empty (“”), “(” + str1 + “)”, or str1 + str2, where str1 and str2 are regular parentheses strings.
For example: “”, “()”, “(())()”, and “(()(()))” are regular parentheses strings.
Examples:
Input: str = “)()(”
Output: 2
Explanation:
For K = 1, S = ()(), which is regular.
For K = 3, S = ()(), which is regular.
Input: S = “())(”
Output: 1
Explanation:
For K = 3, S = (()), which is regular.
Naive Approach: The naive approach is to split the given string str at every possible index(say K) and check whether str[K, N-1] + str[0, K-1] is palindromic or not. If yes then print that particular value of K.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The idea is to observe that if at any index(say K) where the count of closing brackets is greater than the count of opening brackets then that index is the possible index of splitting the string. Below are the steps:
- The partition is only possible when the count the number of opening brackets must be equal to the number of closing brackets. Else we can’t form any partition to balanced the parenthesis.
- Create an auxiliary array(say aux[]) of size length of the string.
- Traverse the given string if character at any index(say i) is ‘(‘ then update aux[i] to 1 else update strong>aux[i] to -1.
- The frequency of the minimum element in the above auxiliary array is the required number of splitting(say at index K) to make S[K…N-1] + S[0…K-1] a regular parenthesis string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countCyclicShifts(string& S, int n)
{
int aux[n] = { 0 };
for ( int i = 0; i < n; ++i) {
if (S[i] == '(' )
aux[i] = 1;
else
aux[i] = -1;
}
int mn = aux[0];
for ( int i = 1; i < n; ++i) {
aux[i] += aux[i - 1];
mn = min(mn, aux[i]);
}
if (aux[n - 1] != 0)
return 0;
int count = 0;
for ( int i = 0; i < n; ++i) {
if (aux[i] == mn)
count++;
}
return count;
}
int main()
{
string S = ")()(" ;
int N = S.length();
cout << countCyclicShifts(S, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countCyclicShifts(String S, int n)
{
int [] aux = new int [n];
for ( int i = 0 ; i < n; ++i)
{
if (S.charAt(i) == '(' )
aux[i] = 1 ;
else
aux[i] = - 1 ;
}
int mn = aux[ 0 ];
for ( int i = 1 ; i < n; ++i)
{
aux[i] += aux[i - 1 ];
mn = Math.min(mn, aux[i]);
}
if (aux[n - 1 ] != 0 )
return 0 ;
int count = 0 ;
for ( int i = 0 ; i < n; ++i)
{
if (aux[i] == mn)
count++;
}
return count;
}
public static void main(String[] args)
{
String S = ")()(" ;
int N = S.length();
System.out.print(countCyclicShifts(S, N));
}
}
|
Python3
def countCyclicShifts(S, n):
aux = [ 0 for i in range (n)]
for i in range ( 0 , n):
if (S[i] = = '(' ):
aux[i] = 1
else :
aux[i] = - 1
mn = aux[ 0 ]
for i in range ( 1 , n):
aux[i] + = aux[i - 1 ]
mn = min (mn, aux[i])
if (aux[n - 1 ] ! = 0 ):
return 0
count = 0
for i in range ( 0 , n):
if (aux[i] = = mn):
count + = 1
return count
S = ")()("
N = len (S)
print (countCyclicShifts(S, N))
|
C#
using System;
class GFG{
static int countCyclicShifts( string S, int n)
{
int [] aux = new int [n];
for ( int i = 0; i < n; ++i)
{
if (S[i] == '(' )
aux[i] = 1;
else
aux[i] = -1;
}
int mn = aux[0];
for ( int i = 1; i < n; ++i)
{
aux[i] += aux[i - 1];
mn = Math.Min(mn, aux[i]);
}
if (aux[n - 1] != 0)
return 0;
int count = 0;
for ( int i = 0; i < n; ++i)
{
if (aux[i] == mn)
count++;
}
return count;
}
public static void Main( string [] args)
{
string S = ")()(" ;
int N = S.Length;
Console.Write(countCyclicShifts(S, N));
}
}
|
Javascript
<script>
function countCyclicShifts(S, n)
{
let aux = [];
for (let i = 0; i < n; ++i)
{
if (S[i] == '(' )
aux[i] = 1;
else
aux[i] = -1;
}
let mn = aux[0];
for (let i = 1; i < n; ++i)
{
aux[i] += aux[i - 1];
mn = Math.min(mn, aux[i]);
}
if (aux[n - 1] != 0)
return 0;
let count = 0;
for (let i = 0; i < n; ++i)
{
if (aux[i] == mn)
count++;
}
return count;
}
let S = ")()(" ;
let N = S.length;
document.write(countCyclicShifts(S, N));
</script>
|
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(N), where N is the length of the string.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
19 Apr, 2021
Like Article
Save Article