Given an unbalanced bracket sequence of ‘(‘ and ‘)’, convert it into a balanced sequence by adding the minimum number of ‘(‘ at the beginning of the string and ‘)’ at the end of the string.
Input: str = “)))()”
Input: str = “())())(())())”
- Let us assume that whenever we encounter with opening bracket the depth increases by one and with a closing bracket the depth decreases by one.
- Find the maximum negative depth in minDep and add that number of ‘(‘ at the beginning.
- Then loop the new sequence to find the number of ‘)’s needed at the end of the string and add them.
- Finally, return the string.
Below is the implementation of the approach:
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2
- Find index of closing bracket for a given opening bracket in an expression
- Find the lexicographical next balanced bracket sequence
- Minimum Cost required to generate a balanced Bracket Sequence
- Minimum number of bracket reversals needed to make an expression balanced
- Number of balanced bracket subsequence of length 2 and 4
- Minimum number of bracket reversals needed to make an expression balanced | Set - 2
- Print the balanced bracket expression using given brackets
- Number of closing brackets needed to complete a regular bracket sequence
- Print all ways to break a string in bracket form
- Construct Binary Tree from String with bracket representation
- Expression contains redundant bracket or not
- Print Bracket Number
- Range Queries for Longest Correct Bracket Subsequence
- Check if expression contains redundant bracket or not | Set 2
- Count distinct regular bracket sequences which are not N periodic
- Minimum Swaps for Bracket Balancing
- Length of Longest Balanced Subsequence
- Length of longest balanced parentheses prefix
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.