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
- Check for balanced parenthesis without using stack
- Find if an expression has duplicate parenthesis or not
- 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
- Number of substrings of a string
- Find substrings that contain all vowels
- Count substrings with same first and last characters
- Replace two substrings (of a string) with each other
- Count all substrings having character K
- Number of substrings divisible by 8 but not by 3
- Count substrings that contain all vowels | SET 2
- Split the string into substrings using delimiter
- Number of substrings with count of each character as k
- Program to print all substrings of a given string
- Count of substrings of a binary string containing K ones
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