Given a string str that consists of lower case English letters and brackets. The task is to reverse the substrings in each pair of matching parentheses, starting from the innermost one. The result should not contain any brackets.
Input: str = “(skeeg(for)skeeg)”
Input: str = “((ng)ipm(ca))”
Approach: This problem can be solved using a stack. First, whenever a ‘(‘ is encountered then push the index of the element into the stack and whenever a ‘)’ is encountered then get the top element of the stack as the latest index and reverse the string between the current index and index from the top of the stack. Follow this for the rest of the string and finally print the updated string.
Below is the implementation of the above approach:
- Number of balanced parenthesis substrings
- Check for balanced parenthesis without using stack
- Find if an expression has duplicate parenthesis or not
- Identify and mark unmatched parenthesis in an expression
- InfyTQ 2019 : Find the position from where the parenthesis is not balanced
- Find maximum depth of nested parenthesis in a string
- Count all substrings having character K
- Count substrings that contain all vowels | SET 2
- Count substrings with same first and last characters
- Number of substrings divisible by 8 but not by 3
- Find substrings that contain all vowels
- Number of substrings of a string
- Replace two substrings (of a string) with each other
- Program to print all substrings of a given string
- Sum of all substrings of a string representing a number | Set 1
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.
Improved By : mohit kumar 29