Given a string S of valid parentheses sequence of length N and an even integer K, the task is to find the valid parentheses sequence of length K which is also subsequence of the given string.
Note: There can be more than one valid sequence, print any of them.
Input: S = “()()()”, K = 4
The string “()()” is a subsequence of length 4 which is a valid parenthesis sequence.
Input: S = “()(())”, K = 6
The string “()(())” is a subsequence of length 6 which is a valid parenthesis sequence.
Naive Approach: The idea is to generate all possible subsequences of length K of the given string and print any of the string having a valid parenthesis sequence.
Time Complexity: O(2N)
Auxiliary Space: O(K)
Efficient Approach: The above approach can be optimized using a Stack. The idea is to traverse the given string and when an open parenthesis character is encountered, push it to the stack else, pop a character from it. Correspondingly, increment the counter every time a character is popped. Follow the below steps to solve the problem:
- Create a stack and the boolean array, initialized to false.
- Traverse the given string and if an opening parenthesis is encountered, push that index into the stack.
- Otherwise, if a closing brace is encountered:
- Pop the top element from the stack
- Increment the counter by 2
- Mark popped and current indices as true.
- If the counter exceeds K, terminate.
- After traversing, append all the characters together, from left to right, which is marked true. Print the resultant string formed.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find maximum depth of nested parenthesis in a string
- Find if an expression has duplicate parenthesis or not
- InfyTQ 2019 : Find the position from where the parenthesis is not balanced
- Calculate weight of parenthesis based on the given conditions
- Identify and mark unmatched parenthesis in an expression
- Check for balanced parenthesis without using stack
- Number of balanced parenthesis substrings
- Reverse substrings between each pair of parenthesis
- Count all indices of cyclic regular parenthesis
- Length of the longest valid substring
- Find minimum length sub-array which has given sub-sequence in it
- k-th missing element in increasing sequence which is not present in a given sequence
- Replace every consonant sequence with its length in the given string
- Maximum Length of Sequence of Sums of prime factors generated by the given operations
- Minimum length of Run Length Encoding possible by removing at most K characters from a given string
- Check if a given string is a valid number (Integer or Floating Point) | SET 1(Basic approach)
- Check if a given string is a valid number (Integer or Floating Point) in Java
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
- Program to generate all possible valid IP addresses from given string
- Check if the given push and pop sequences of Stack is valid or not
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.