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 the lexicographical next balanced bracket sequence
- Number of closing brackets needed to complete a regular bracket sequence
- Convert String into Binary Sequence
- Convert a sentence into its equivalent mobile numeric keypad sequence
- Number of balanced bracket subsequence of length 2 and 4
- Print the balanced bracket expression using given brackets
- Minimum number of bracket reversals needed to make an expression balanced | Set - 2
- Minimum number of bracket reversals needed to make an expression balanced
- Find index of closing bracket for a given opening bracket in an expression
- Look-and-Say Sequence
- De Bruijn sequence | Set 1
- Sequence Alignment problem
- Create a sequence whose XOR of elements is y
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.