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.
Input: str = “)()(”
For K = 1, S = ()(), which is regular.
For K = 3, S = ()(), which is regular.
Input: S = “())(”
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:
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(N), where N is the length of the string.
- Count of cyclic permutations having XOR with other binary string as 0
- Maximize count of corresponding same elements in given permutations using cyclic rotations
- Count number of ways to convert string S to T by performing K cyclic shifts
- Count number of indices such that s[i] = s[i+1] : Range queries
- Count triplet of indices (i, j, k) such that XOR of elements between [i, j) equals [j, k]
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Number of balanced parenthesis substrings
- Reverse substrings between each pair of parenthesis
- Calculate weight of parenthesis based on the given conditions
- Identify and mark unmatched parenthesis in an expression
- Find maximum depth of nested parenthesis in a string
- InfyTQ 2019 : Find the position from where the parenthesis is not balanced
- Minimum moves to reach from i to j in a cyclic string
- Sort a string lexicographically using triple cyclic shifts
- Sort permutation of N natural numbers using triple cyclic right swaps
- Check if a decreasing Array can be sorted using Triple cyclic shift
- Find initial sequence that produces a given Array by cyclic increments upto index P
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Rearrange a string according to the given indices
- Sum of all the Composite Numbers from Odd indices of the given array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.