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
- Reverse the substrings of the given String according to the given Array of indices
- Find if an expression has duplicate parenthesis or not
- Check for balanced parenthesis without using stack
- Identify and mark unmatched parenthesis in an expression
- Find maximum depth of nested parenthesis in a string
- InfyTQ 2019 : Find the position from where the parenthesis is not balanced
- Count substrings with same first and last characters
- Count all substrings having character K
- 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
- Count substrings that contain all vowels | SET 2
- Program to print all substrings of a given string
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