-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateParenthesis.java
More file actions
35 lines (31 loc) · 996 Bytes
/
GenerateParenthesis.java
File metadata and controls
35 lines (31 loc) · 996 Bytes
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.*;
public class GenerateParenthesis {
static Map<Integer, ArrayList<String>> mp;
public static void strGen(int j, int i, ArrayList<String> al) {
for(int k=0;k<mp.get(i-j-1).size();k++) {
String str = "(" + mp.get(i-j-1).get(k) + ")";
for(int m=0;m<mp.get(j).size();m++) {
String s2 = str + mp.get(j).get(m);
al.add(s2);
}
}
}
public static void generate(int n) {
mp.put(0, new ArrayList<>(Arrays.asList("")));
mp.put(1, new ArrayList<>(
Arrays.asList("()")));
for(int i=2;i<=n;i++) {
ArrayList<String> al = new ArrayList<>();
for(int j=0;j<i;j++) {
strGen(j,i, al);
}
mp.put(i, al);
}
}
public static void main(String args[]) {
mp = new HashMap<>();
generate(3);
System.out.println(mp);
System.out.println(mp.get(3));
}
}