Given a bracket sequence of even length. The task is to find how many ways are there to make balanced bracket subsequences from the given sequence of length 2 and 4.
The sequence () is a bracket sequence of length 2. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
Note: “1” represent opening bracket and “2” represents closing bracket
Input: 1 2 1 1 2 2 Output: 14 Input: 1 2 1 2 Output: 4
Approach: For the length 2 subsequence we will just see for each 1 how many 2 are there which can be easily achievable by taking a simple suffix sum of the number of 2 ‘s present in the sequence. So first take the suffix sum of the number of 2 ‘s present in the sequence.
For length 4 subsequence there are 2 choices:
First one is 1 1 2 2
And the other one is 1 2 1 2.
- For the 1st one, run a loop from left to right to get the first open bracket an inner loop to get the next opening bracket now from the suffix array we can get the count of 2 after the 2nd opening bracket and calculate the number of subsequence by count*(count-1)/2 because for each closing bracket for inner opening bracket we get count-1 number of choices for the 1st opening bracket.
- For the 2nd type of subsequence, we again run a loop from left to right to get the first open bracket an inner loop to get the next opening bracket. Then we calculate the number of subsequence by getting the count of 2’s after the 1st opening bracket by simply subtracting the count of 2’s after the 2nd opening bracket and the count of 2’s after the 1st opening bracket and multiplying it with the count of 2’s after the 2nd opening bracket (we get all these values from the frequency suffix array).
Below is the implementation of the above approach:
Time Complexity: O(N*N)
Auxiliary complexity : O(N)
- 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
- Find index of closing bracket for a given opening bracket in an expression
- Minimum number of bracket reversals needed to make an expression balanced
- Number of balanced bracket expressions that can be formed from a string
- Minimum number of bracket reversals needed to make an expression balanced | Set - 2
- Find the lexicographical next balanced bracket sequence
- Print the balanced bracket expression using given brackets
- Minimum Cost required to generate a balanced Bracket Sequence
- Convert an unbalanced bracket sequence to a balanced sequence
- Range Queries for Longest Correct Bracket Subsequence
- Range Queries for Longest Correct Bracket Subsequence Set | 2
- Length of Longest Balanced Subsequence
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Number of closing brackets needed to complete a regular bracket sequence
- Print Bracket Number
- Maximum length of balanced string after swapping and removal of characters
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Expression contains redundant bracket or not
- Check if expression contains redundant bracket or not | Set 2
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.