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:
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.
- Cost to Balance the parentheses
- Find an equal point in a string of brackets
- Binary tree to string with brackets
- Print string after removing all (“10” or “01”) from the binary string
- Number of closing brackets needed to complete a regular bracket sequence
- Check if two expressions with brackets are same
- Print the balanced bracket expression using given brackets
- Check for Balanced Brackets in an expression (well-formedness) using Stack
- Character replacement after removing duplicates from a string
- Find the resultant String after replacing X with Y and removing Z
- Check if string remains palindrome after removing given number of characters
- Check if string is palindrome after removing all consecutive duplicates
- Minimum sum of squares of character counts in a given string after removing k characters
- Print reverse string after removing vowels
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Subsequence queries after removing substrings
- Minimize ASCII values sum after removing all occurrences of one character
- Maximum points covered after removing an Interval
- Reduce the number to minimum multiple of 4 after removing the digits
- Maximum non-repeating characters after removing K characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.