Given an integer N, the task is to find the number of distinct bracket sequences that can be formed using 2 * N brackets such that the sequence is not N-periodic.
A bracket sequence str of length 2 * N is said to be N-periodic if the sequence can be split into two equal substrings having same regular bracket sequence.
A regular bracket sequence is a sequence in the following way:
- An empty string is a regular bracket sequence.
- If s & t are regular bracket sequences, then s + t is a regular bracket sequence.
Input: N = 3
There will be 5 distinct regular bracket sequences of length 2 * N = ()()(), ()(()), (())(), (()()), ((()))
Now, none of the sequences are N-periodic. Therefore, the output is 5.
Input: N = 4
There will be 14 distinct regular bracket sequences of length 2*N which are
()()()(), ()()(()), ()(())(), ()(()()), ()((())), (())()(), (())(()), (()())(), (()()()), (()(())), ((()))(), ((())()), ((()())), (((())))
Out of these 14 regular sequences, two of them are N periodic which are
()()()() and (())(()). They have a period of N.
Therefore, the distinct regular bracket sequences of length 2 * N which are not N-periodic are 14 – 2 = 12.
Approach: The idea is to calculate the total number of regular bracket sequences possible of length 2 * N and then to subtract the number of bracket sequences which are N-periodic from it. Below are the steps:
- To find the number of regular bracket sequences of length 2*N, use the Catalan number formula.
- For a sequence of length 2*N to be N periodic, N should be even because if N is odd then the sequence of length 2*N cannot be a regular sequence and have a period of N at the same time.
- Since the concatenation of two similar non-regular bracket sequences cannot make a sequence regular, so both subsequences of length N should be regular.
- Reduce the number of regular bracket sequences of length N(if N is even) from the number of regular bracket sequences of length 2*N to get the desired result.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: 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.
- 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
- Number of closing brackets needed to complete a regular bracket sequence
- Arithmetic Sequences - Sequences and Series | Class 11 Maths
- Smallest number greater than X which is K-periodic
- Expression contains redundant bracket or not
- Check if expression contains redundant bracket or not | Set 2
- Count of sub-sequences which satisfy the given condition
- Check if the given string is K-periodic
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- Minimum number of swaps required to make the string K periodic
- Find the maximum possible value for the given periodic function
- Minimum replacements required to obtain a K-periodic palindromic string
- Distinct strings such that they contains given strings as sub-sequences
- Find if it is possible to make a binary string which contanins given number of "0", "1" , "01" and "10" as sub sequences
- Number of sequences which has HEAD at alternate positions to the right of the first HEAD
- Check if two same sub-sequences exist in a string or not
- Minimum number of bracket reversals needed to make an expression balanced
- Range Queries for Longest Correct Bracket Subsequence
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.