Given a string of characters with opening and closing brackets. The task is to remove extra brackets from string and balance it.
Input: str = “gau)ra)v(ku(mar(rajput))”
Input: str = “1+5)+5+)6+(5+9)*9”
- Start traversing from left to right.
- Check if the element at current index is an opening bracket ‘(‘ then print that bracket and increment count.
- Check if the element at current index is a closing bracket ‘)’ and if the count is not equal to zero then print it and decrement the count.
- Check if there is any element other than brackets at the current index in the string then print it.
- And in last if the count is not equal to zero then print ‘)’ equal to the number of the count to balance the string.
Below is the implementation of above approach:
- Binary tree to string with brackets
- Find an equal point in a string of brackets
- Removing punctuations from a given string
- Removing spaces from a string using Stringstream
- Modify string by removing vowels in between two consonants
- Ways of transforming one string to other by removing 0 or more characters
- Character replacement after removing duplicates from a string
- Print reverse string after removing vowels
- Minimize the length of string by removing occurrence of only one character
- Reduce the string by removing K consecutive identical characters
- Lexicographically smallest string formed by removing at most one character
- Find one extra character in a string
- Remove extra spaces from a string
- Minimum sum of squares of character counts in a given string after removing k characters
- Replace all occurrences of string AB with C without using extra space
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.