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
- Count pair of strings whose concatenation of substrings form a palindrome
- Find if an expression has duplicate parenthesis or not
- Check for balanced parenthesis without using stack
- Calculate weight of parenthesis based on the given conditions
- Count all indices of cyclic regular parenthesis
- 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 substrings with same first and last characters
- Count substrings that contain all vowels | SET 2
- Number of substrings divisible by 8 but not by 3
- Replace two substrings (of a string) with each other
- Count all substrings having character K
- Find substrings that contain all vowels
- Number of substrings of a string
- Number of substrings that start with "geeks" and end with "for"
- Lexicographical concatenation of all substrings of a string
- Queries for frequencies of characters in substrings
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.
Improved By : mohit kumar 29