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 of Reverse Bitonic Substrings in a given String
- 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
- 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
- Find substrings that contain all vowels
- Count all substrings having character K
- Count substrings that contain all vowels | SET 2
- Replace two substrings (of a string) with each other
- Count of substrings containing only the given character
- Number of substrings of a string
- Number of substrings divisible by 8 but not by 3
- Count of substrings having all distinct 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 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