Skip to content

Commit 477cdc9

Browse files
authored
Create week6 22. Generate Parentheses
1 parent ea5b95b commit 477cdc9

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

week6 22. Generate Parentheses

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Solution {
2+
public List<String> generateParenthesis(int n) {
3+
List<String> res = new ArrayList<>();
4+
helper (res, new String(), n, 0);
5+
return res;
6+
}
7+
public void helper (List<String> res, String str, int n, int counter){
8+
if(counter == 0 && str.length() == 2 * n){
9+
res.add(new String(str));
10+
return;
11+
}
12+
if(counter < 0 || counter > n || str.length() > 2*n){
13+
return;
14+
}
15+
if(counter > 0 && counter < n){
16+
helper(res, str + "(", n, counter + 1);
17+
helper(res, str + ")", n, counter - 1);
18+
}else if (counter == 0){
19+
helper(res, str + "(", n, counter + 1);
20+
}else {
21+
helper(res, str + ")", n, counter - 1);
22+
}
23+
return;
24+
}
25+
}

0 commit comments

Comments
 (0)