Given a bracket sequence or in other words a string S of length n, consisting of characters ‘(‘ and ‘)’. Find the length of the maximum correct bracket subsequence of sequence for a given query range. Note: A correct bracket sequence is the one that has matched bracket pairs or which contains another nested correct bracket sequence. For e.g (), (()), ()() are some correct bracket sequence.
Input : S = ())(())(())( Start Index of Range = 0, End Index of Range = 11 Output : 10 Explanation: Longest Correct Bracket Subsequence is ()(())(()) Input : S = ())(())(())( Start Index of Range = 1, End Index of Range = 2 Output : 0
Approach: In the Previous post (SET 1) we discussed a solution that works in O(long) for each query, now is this post we will go to see a solution that works in O(1) for each query.
The idea is based on the Post length of the longest valid balanced substring If we marked indexes of all Balanced parentheses/brackets in a temporary array (here we named it BCP, BOP ) then we answer each query in O(1) time.
stack is used to get the index of balance bracket. Travese a string from 0 ..to n IF we seen a closing bracket, ( i.e., str[i] = ')' && stack is not empty ) Then mark both "open & close" bracket indexes as 1. BCP[i] = 1; BOP[stk.top()] = 1; And At last, stored cumulative sum of BCP & BOP Run a loop from 1 to n BOP[i] +=BOP[i-1], BCP[i] +=BCP[i-1]
Now you can answer each query in O(1) time
(BCP[e] - BOP[s-1]])*2;
Below is the implementation of the above idea.
Maximum Length Correct Bracket Subsequence between 5 and 11 = 4 Maximum Length Correct Bracket Subsequence between 4 and 5 = 0 Maximum Length Correct Bracket Subsequence between 1 and 5 = 2
The time complexity for each query is O(1).
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.
- Range Queries for Longest Correct Bracket Subsequence
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2
- Find index of closing bracket for a given opening bracket in an expression
- Minimum number of bracket reversals needed to make an expression balanced
- Construct Binary Tree from String with bracket representation
- Expression contains redundant bracket or not
- Print Bracket Number
- Print next greater number of Q queries
- Length of the longest valid substring
- Range Sum and Update in Array : Segment Tree using Stack
- Construct BST from given preorder traversal | Set 2
- Iterative Postorder Traversal | Set 1 (Using Two Stacks)
- Iterative Postorder Traversal | Set 2 (Using One Stack)
- Largest Rectangular Area in a Histogram | Set 2
- Largest Rectangular Area in a Histogram | Set 1
- One97 Interview Experience | Set 3 (Backend/Node js Developer)
- Flipkart Interview Experience | Set 24
- MAQ Software Interview Experience | Set 10 (On-Campus)
- Walmart labs interview | Set 4 (For Senior Software Engineer)
- MakeMyTrip Interview Experience | Set 9 (Off-Campus for Sr. Android developer))
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.