-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateParentheses.java
More file actions
46 lines (41 loc) · 1.18 KB
/
GenerateParentheses.java
File metadata and controls
46 lines (41 loc) · 1.18 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
/*
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
Example 1:
Input: n = 3
Output: ["((()))","(()())","(())()","()(())","()()()"]
Example 2:
Input: n = 1
Output: ["()"]
*/
class Solution {
public List<String> generateParenthesis(int n) {
List<String> res = new ArrayList<>();
help(2*n, "", res);
return res;
}
private void help(int n, String str, List<String> res) {
if(n == 0){
if(isValid(str))
res.add(str);
return;
}
help(n-1, str + "(", res);
help(n-1, str + ")", res);
}
private boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for(int i = 0;i<s.length();i++){
char c = s.charAt(i);
//open bracket ---> push to stack
if(c == '(')
stack.push(c);
//closed bracket ---> if match to top then pop
else{
if(stack.isEmpty() || ')' != c) return false;
if(')' == c)
stack.pop();
}
}
return stack.isEmpty(); //if stack is empty then valid
}
}