-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutationPractise.java
More file actions
41 lines (36 loc) · 1.24 KB
/
PermutationPractise.java
File metadata and controls
41 lines (36 loc) · 1.24 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
import java.util.ArrayList;
public class PermutationPractise {
public static void main(String[] args) {
String[][] data = { { "A", "B", "C" }, { "1", "2" }, { "3", "4", "5" } };
ArrayList<ArrayList<String>> permutation = new ArrayList<ArrayList<String>>();
permutation = permute(data, permutation, 0);
System.out.println("Size : " + permutation.size());
for (ArrayList<String> temp : permutation) {
System.out.println(temp);
}
}
static ArrayList<ArrayList<String>> permute(String[][] data,
ArrayList<ArrayList<String>> permutation, int i) {
if (i == 0) {
for (int j = 0; j < data[i].length; j++) {
ArrayList<String> temp = new ArrayList<String>();
temp.add(data[i][j]);
permutation.add(temp);
}
return permute(data, permutation, i + 1);
} else if (i < data.length) {
ArrayList<ArrayList<String>> tempPermutation = new ArrayList<ArrayList<String>>();
for (int j = 0; j < data[i].length; j++) {
for (int k = 0; k < permutation.size(); k++) {
@SuppressWarnings("unchecked")
ArrayList<String> temp = (ArrayList<String>) permutation
.get(k).clone();
temp.add(data[i][j]);
tempPermutation.add(temp);
}
}
return permute(data, tempPermutation, i + 1);
}
return permutation;
}
}