-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAllPossibleCombinations.java
78 lines (62 loc) · 2.24 KB
/
AllPossibleCombinations.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
public class AllPossibleCombinations {
public GrowableArray all_anagrams;
public int[] tracking;
public String the_string;
public AllPossibleCombinations(String s){
all_anagrams = new GrowableArray();
tracking = new int[s.length()];
the_string = s;
}
public void generate_all_possible_anagrams(int idx, int strlen) {
if(idx == strlen){
String str = "";
Boolean add_space = false;
for(int i = 0; i<the_string.length(); i++) {
if(tracking[i] == 1){
str+= Character.toString(the_string.charAt(i)) ;
add_space = true;
}
}
if(add_space) str += " ";
add_space = false;
for(int i = 0; i<the_string.length(); i++) {
if(tracking[i] == 2){
str+= Character.toString(the_string.charAt(i)) ;
add_space = true;
}
}
if(add_space) str += " ";
add_space = false;
for(int i = 0; i<the_string.length(); i++) {
if(tracking[i] == 3){
str+= Character.toString(the_string.charAt(i)) ;
add_space = true;
}
}
if(add_space) str += " ";
str = str.trim();
all_anagrams.insertKey(str);
return;
}
tracking[idx] = 1;
generate_all_possible_anagrams((idx + 1), strlen);
tracking[idx] = 2;
generate_all_possible_anagrams((idx + 1), strlen);
tracking[idx] = 3;
generate_all_possible_anagrams((idx + 1), strlen);
}
public String[] generate_all_possible_anagrams(){
this.generate_all_possible_anagrams(0, this.the_string.length());
int counter = 0;
for (Object string : this.all_anagrams.arr_elements) {
if (string!=null) {
counter+=1;
}
}
String AllPossibleCombinations[] = new String[counter];
for (int i = 0; i < counter; i++) {
AllPossibleCombinations[i] = (String)this.all_anagrams.arr_elements[i];
}
return AllPossibleCombinations;
}
}