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 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:
- Length of Longest Balanced Subsequence
- 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
- Minimum number of bracket reversals needed to make an expression balanced
- Print the balanced bracket expression using given brackets
- Range Queries for Longest Correct Bracket Subsequence
- Range Queries for Longest Correct Bracket Subsequence Set | 2
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Find index of closing bracket for a given opening bracket in an expression
- Length of longest balanced parentheses prefix
- Maximum length of balanced string after swapping and removal of characters
- Print Bracket Number
- Repeated subsequence of length 2 or more
- Count subsequence of length three in a given string
- Maximum length subsequence possible of the form R^N K^N
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.