-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay42.java
More file actions
47 lines (37 loc) · 1.26 KB
/
Copy pathDay42.java
File metadata and controls
47 lines (37 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*Number of Valid Parentheses
You are given a number n, your task is to find the number of all the valid parentheses expressions of that length using only "(" and ")" brackets.
An input string of parentheses is valid if :
Open brackets must be closed in correct order.
Every close bracket has a corresponding open bracket.
For example - "()()" or "(())" are valid while ")()(" or "))((" are invalid parentheses expressions.
Examples:
Input: n = 2
Output: 1
Explanation: There is only one possibe valid expressions of length 2 i.e., "()".
Input: n = 4
Output: 2
Explanation: Possibe valid expressions of length 4 are "(())" and "()()".
Input: n = 6
Output: 5
Explanation: Possibe valid expressions of length 6 are "((()))", "(())()", "()(())", "()()()" and "(()())". */
/*class Solution {
int findWays(int n) {
// If length is odd, no valid parentheses possible
if (n % 2 != 0) {
return 0;
}
int pairs = n / 2;
long[] dp = new long[pairs + 1];
dp[0] = 1; // Base case
for (int i = 1; i <= pairs; i++) {
for (int j = 0; j < i; j++) {
dp[i] += dp[j] * dp[i - 1 - j];
}
}
return (int) dp[pairs];
}
}
*/
/*⏱️ Complexity
Time: O((n/2)²)
Space: O(n/2) */