Find the lexicographical next balanced bracket sequence
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.
Examples:
Input: str = “(())”
Output: ()()
Input: str = “((()))”
Output: (()())
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:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to find the lexicographically // next balanced bracket // expression if possible string next_balanced_sequence(string& s) { string next = "-1" ; int length = s.size(); int depth = 0; for ( int i = length - 1; i >= 0; --i) { // Decrement the depth for // every opening bracket if (s[i] == '(' ) depth--; // Increment for the // closing brackets else depth++; // Last opening bracket if (s[i] == '(' && depth > 0) { depth--; int open = (length - i - 1 - depth) / 2; int close = length - i - 1 - open; // Generate the required string next = s.substr(0, i) + ')' + string(open, '(' ) + string(close, ')' ); break ; } } return next; } // Driver code int main() { string s = "((()))" ; cout << next_balanced_sequence(s); return 0; } |
Java
// Java implementation of the approach class Sol { // makes a string containing char d // c number of times static String string( int c, char d) { String s = "" ; for ( int i = 0 ; i < c; i++) s += d; return s; } // Function to find the lexicographically // next balanced bracket // expression if possible static String next_balanced_sequence(String s) { String next = "-1" ; int length = s.length(); int depth = 0 ; for ( int i = length - 1 ; i >= 0 ; --i) { // Decrement the depth for // every opening bracket if (s.charAt(i) == '(' ) depth--; // Increment for the // closing brackets else depth++; // Last opening bracket if (s.charAt(i) == '(' && depth > 0 ) { depth--; int open = (length - i - 1 - depth) / 2 ; int close = length - i - 1 - open; // Generate the required String next = s.substring( 0 , i) + ')' + string(open, '(' ) + string(close, ')' ); break ; } } return next; } // Driver code public static void main(String args[]) { String s = "((()))" ; System.out.println(next_balanced_sequence(s)); } } // This code is contributed by Arnab Kundu |
Python3
# Python3 implementation of the approach # Function to find the lexicographically # next balanced bracket # expression if possible def next_balanced_sequence(s) : next = "-1" ; length = len (s); depth = 0 ; for i in range (length - 1 , - 1 , - 1 ) : # Decrement the depth for # every opening bracket if (s[i] = = '(' ) : depth - = 1 ; # Increment for the # closing brackets else : depth + = 1 ; # Last opening bracket if (s[i] = = '(' and depth > 0 ) : depth - = 1 ; open = (length - i - 1 - depth) / / 2 ; close = length - i - 1 - open ; # Generate the required string next = s[ 0 : i] + ')' + open * '(' + close * ')' ; break ; return next ; # Driver code if __name__ = = "__main__" : s = "((()))" ; print (next_balanced_sequence(s)); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System; class GFG { // makes a string containing char d // c number of times static String strings( int c, char d) { String s = "" ; for ( int i = 0; i < c; i++) s += d; return s; } // Function to find the lexicographically // next balanced bracket // expression if possible static String next_balanced_sequence(String s) { String next = "-1" ; int length = s.Length; int depth = 0; for ( int i = length - 1; i >= 0; --i) { // Decrement the depth for // every opening bracket if (s[i] == '(' ) depth--; // Increment for the // closing brackets else depth++; // Last opening bracket if (s[i] == '(' && depth > 0) { depth--; int open = (length - i - 1 - depth) / 2; int close = length - i - 1 - open; // Generate the required String next = s.Substring(0, i) + ')' + strings(open, '(' ) + strings(close, ')' ); break ; } } return next; } // Driver code public static void Main(String []args) { String s = "((()))" ; Console.WriteLine(next_balanced_sequence(s)); } } // This code is contributed by Princi Singh |
Javascript
<script> // Javascript program for the above approach // makes a string containing char d // c number of times function string(c, d) { let s = "" ; for (let i = 0; i < c; i++) s += d; return s; } // Function to find the lexicographically // next balanced bracket // expression if possible function next_balanced_sequence(s) { let next = "-1" ; let length = s.length; let depth = 0; for (let i = length - 1; i >= 0; --i) { // Decrement the depth for // every opening bracket if (s[i] == '(' ) depth--; // Increment for the // closing brackets else depth++; // Last opening bracket if (s[i] == '(' && depth > 0) { depth--; let open = (length - i - 1 - depth) / 2; let close = length - i - 1 - open; // Generate the required String next = s.substr(0, i) + ')' + string(open, '(' ) + string(close, ')' ); break ; } } return next; } // Driver Code let s = "((()))" ; document.write(next_balanced_sequence(s)); // This code is contributed by sanjoy_62. </script> |
(()())
Time Complexity: O(n)
Auxiliary Space: O(1)
Please Login to comment...