-
Notifications
You must be signed in to change notification settings - Fork 46
/
_22.java
35 lines (32 loc) · 996 Bytes
/
_22.java
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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* LeetCode 22 - Generate Parentheses
* <p>
* DP approach
* <p>
* To avoid generating duplicate strings, we enumerate the first part which is surrounded by a pair of (), i.e.
* <p>
* ( [subprob 1] ) [subprob 2]
* <p>
* Note that both sub problems can be empty, i.e., empty string.
*/
public class _22 {
static Map<Integer, List<String>> map = new HashMap<>();
public List<String> generateParenthesis(int n) {
if (map.containsKey(n)) return map.get(n);
List<String> ans = new ArrayList<>();
if (n == 0) ans.add("");
else {
for (int i = 1; i <= n; i++) {
for (String l : generateParenthesis(i - 1))
for (String r : generateParenthesis(n - i)) // will be
ans.add(String.format("(%s)%s", l, r));
}
}
map.put(n, ans);
return ans;
}
}