Given a number n find the number of valid parentheses expressions of that length.
Input: 2 Output: 1 There is only possible valid expression of length 2, "()" Input: 4 Output: 2 Possible valid expression of length 4 are "(())" and "()()" Input: 6 Output: 5 Possible valid expressions are ((())), ()(()), ()()(), (())() and (()())
This is mainly an application of Catalan Numbers. Total possible valid expressions for input n is n/2’th Catalan Number if n is even and 0 if n is odd.
Below given is the implementation :
Total possible expressions of length 6 is 5
Time Complexity: O(n)
This article is contributed by Sachin. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
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.
- Count pairs of parentheses sequences such that parentheses are balanced
- Minimum number of Parentheses to be added to make it valid
- Check if the given Binary Expressions are valid
- Length of longest balanced parentheses prefix
- Number of ways to insert two pairs of parentheses into a string of N characters
- Print all combinations of balanced parentheses
- Remove Invalid Parentheses
- Pairs involved in Balanced Parentheses
- Check for balanced parentheses in an expression | O(1) space | O(N^2) time complexity
- Check for balanced parentheses in an expression | O(1) space
- Check for balanced parentheses in Python
- Number of balanced bracket expressions that can be formed from a string
- Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
- Program to evaluate simple expressions
- What is an Expression and What are the types of Expressions?
- Count valid pairs in the array satisfying given conditions
- Check if the given Prufer sequence is valid or not
- Check whether the triangle is valid or not if angles are given
- Check whether Quadrilateral is valid or not if angles are given
- Check whether triangle is valid or not if three points are given