The only line of input contains an integer ‘N’ representing the given number of parentheses.
The output contains all possible valid parentheses printed in different lines.
The order in which different combinations of well-formed parentheses are printed doesn't matter.
1 <= N <= 10
Time Limit: 1sec
To form all the sequences of balanced bracket subsequences with N pairs. So there are N opening brackets and N closing brackets.
So the subsequence will be of length 2 * N. There is a simple idea, the i’th character can be ‘{‘ if and only if the count of ‘{‘ till i’th is less than N and i’th character can be ‘}’ if and only if the count of ‘{‘ is greater than the count of ‘}’ till index i. If these two cases are followed then the resulting subsequence will always be balanced. So form the recursive function using the above two cases.
Below is the algorithm:
We can generate 2 ^ 2n sequences of ‘{‘ and ‘}’ characters. Then, we will check if each one is valid.
To generate all sequences, we use recursion. All sequences of length N are just ‘{‘ plus all sequences of length N - 1, and then ‘}’ plus all sequences of length N - 1.
Instead of adding ‘{‘ or ‘}’ every time, we’ll add them when we know it will remain a valid sequence. We can do this by keeping track of the number of opening and closing brackets we have placed so far.
We can start an opening bracket if we still have one (of N) left to place. And we can start a closing bracket if it would not exceed the number of opening brackets.
Cakes
1-3 Palindrome
8-Queen Problem
Search Pattern (KMP - Algorithm)
Search Pattern (KMP - Algorithm)
Search Pattern (KMP - Algorithm)
RNA or DNA