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
- Check for balanced parentheses in an expression
- 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
- Find the length of the median of a Triangle if length of sides are given
- 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