Given a balanced bracket sequence as a string str containing character ‘(‘ or ‘)’, the task is to find the next lexicographical order balanced sequence if possible else print -1.
Input: str = “(())”
Input: str = “((()))”
Approach: First find the rightmost opening bracket which we can replace it by a closing bracket to get the lexicographically larger bracket string. The updated string might not be balanced, we can fill the remaining part of the string with the lexicographically minimal one: i.e. first with as much opening brackets as possible, and then fill up the remaining positions with closing brackets. In other words we try to leave a long as possible prefix unchanged, and the suffix gets replaced by the lexicographically minimal one.
To find this position, we can iterate over the character from right to left, and maintain the balance depth of open and closing brackets. When we meet an opening brackets, we will decrement depth, and when we meet a closing bracket, we increase it. If we are at some point meet an opening bracket, and the balance after processing this symbol is positive, then we have found the rightmost position that we can change. We change the symbol, compute the number of opening and closing brackets that we have to add to the right side, and arrange them in the lexicographically minimal way.
If we find no suitable position, then this sequence is already the maximal possible one, and there is no answer.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket
- Convert an unbalanced bracket sequence to a balanced sequence
- Minimum Cost required to generate a balanced Bracket Sequence
- Number of balanced bracket subsequence of length 2 and 4
- Print the balanced bracket expression using given brackets
- Number of closing brackets needed to complete a regular bracket sequence
- Find next Smaller of next Greater in an array
- Find the k-th string in lexicographical order consisting of n-2 X's and 2 Y's
- Minimum operation required to make a balanced sequence
- Find original sequence from Array containing the sequence merged many times in order
- Print all longest common sub-sequences in lexicographical order
- Sort the words in lexicographical order in Python
- Lexicographical concatenation of all substrings of a string
- Lexicographical Maximum substring of string
- Print all distinct circular strings of length M in lexicographical order
- K-th lexicographical string of given length
- Print all the combinations of a string in lexicographical order
- Lexicographical smallest alternate Array
- Largest lexicographical string with at most K consecutive elements
- Generate all numbers up to N in Lexicographical Order
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.