Given a bracket sequence or in other words a string S of length n, consisting of characters ‘(‘ and ‘)’. Find length of the maximum correct bracket subsequence of sequence for a given query range. Note: A correct bracket sequence is the one that have 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
Segment Trees can be used to solve this problem efficiently
At each node of the segment tree, we store the following:
1) a - Number of correctly matched pairs of brackets. 2) b - Number of unused open brackets. 3) c - Number of unused closed brackets.
(unused open bracket – means they can’t be matched with any closing bracket, unused closed bracket – means they can’t be matched with any opening bracket, for e.g S = )( contains an unused open and an unused closed bracket)
For each interval [L, R], we can match X number of unused open brackets ‘(‘ in interval [L, MID] with unused closed brackets ‘)’ in interval [MID + 1, R] where
X = minimum(number of unused ‘(‘ in [L, MID], number of unused ‘)’ in [MID + 1, R])
Hence, X is also the number of correctly matched pairs built by combination.
So, for interval [L, R]
1) Total number of correctly matched pairs becomes the sum of correctly matched pairs in left child and correctly matched pairs in right child and number of combinations of unused ‘(‘ and unused ‘)’ from left and right child respectively.
a[L, R] = a[L, MID] + a[MID + 1, R] + X
2) Total number of unused open brackets becomes the sum of unused open brackets in left child and unused open brackets in right child minus X (minus – because we used X unused ‘(‘ from left child to match with unused ‘) from right child).
a[L, R] = b[L, MID] + b[MID + 1, R] - X
3) Similarly, for ununsed closed brackets, following relation holds.
a[L, R] = c[L, MID] + c[MID + 1, R] - X
where a, b and c are the representations described above for each node to be stored in.
Below is the implementation of above approach in C++.
Maximum Length Correct Bracket Subsequence between 0 and 11 = 10 Maximum Length Correct Bracket Subsequence between 1 and 2 = 0
Time complexity for each query is O(logN), where N is the size of string.
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 Set | 2
- Find index of closing bracket for a given opening bracket in an expression
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2
- Array range queries over range queries
- Number of balanced bracket subsequence of length 2 and 4
- Queries to find longest subsequence having no smilar adjacent elements with updates
- Binary Indexed Tree : Range Update and Range Queries
- Queries for elements having values within the range A to B in the given index range using Segment Tree
- Range Queries to count elements lying in a given Range : MO's Algorithm
- Check if the Depth of Parentheses is correct in the given String
- Minimum number of bracket reversals needed to make an expression balanced
- Number of closing brackets needed to complete a regular bracket sequence
- Expression contains redundant bracket or not
- Minimum number of bracket reversals needed to make an expression balanced | Set - 2
- Check if expression contains redundant bracket or not | Set 2
- Minimum Cost required to generate a balanced Bracket Sequence
- Count distinct regular bracket sequences which are not N periodic
- Print all ways to break a string in bracket form
- Convert an unbalanced bracket sequence to a balanced sequence