I am trying to get all the possible combinations of words input by user. Such as words input like aa, bb, cc
should give
aa
bb
cc
aabb
aacc
bbaa
bbcc
ccaa
ccbb
aabbcc
aaccbb
bbaacc
bbccaa
ccbbaa
ccaabb
They can in any order but preferably in sorted order.
I have been trying to accomplish this for past hour and I think I am confused about something that I can't figure out and keep going in circles. Can someone please point me in the right direction.
The code so far is
import java.util.Arrays;
import java.util.Scanner;
public class WordCombination {
private static String[] wordlist;
private static String[] wordcomb;
public static void main(String[] argv){
Scanner a = new Scanner(System.in);
System.out.print("Enter Words:");
setWords(a.nextLine());
creatCombinations();
}
private static void setWords(String words){
System.out.println("Entered words: " + words);
wordlist = words.split("\\s+");
Arrays.sort(wordlist);
System.out.println(Arrays.toString(wordlist));
}
private static void creatCombinations(){
int size = wordlist.length;
int perm = (int) Math.pow(size, size);
int c = 1;
wordcomb = new String[perm];
String word = "";
/*
for(int i = 0; i < size; i++){
word = wordlist[i];
for(int j = 0; j < size; j++){
word += wordlist[j];
System.out.println(c + ": " + word + ".com");
c++;
}
word = "";
}*/
int l = 0;
for(int i = 0; i < size; i++){
int permj = (i+1) * size;
System.out.println("------------------> " + permj + " permutations for " + (i + 1) + " words combination");
int iterations = permj * (i+1);
System.out.println(" Iterations: " + iterations);
word = wordlist[i];
for(int j = 0; j < permj; j++){
for(int k = 0; k < i+1; k++){
int permi = i * size;
int index = permi + (k%permj);
wordcomb[c-1] += "" + wordlist[l%size];
out(l%size + "");
l++;
}
System.out.println(c + ": " + wordcomb[c-1]);
c++;
}
word = "";
}
}
private static void out(String s){
System.out.println(s);
}
}
*Edit: The output I am getting is *
Enter Words:1 2 3
Entered words: 1 2 3
[1, 2, 3]
------------------> 3 permutations for 1 words combination
Iterations: 3
0
1: 1
1
2: 2
2
3: 3
------------------> 6 permutations for 2 words combination
Iterations: 12
Enter Words:aa bb cc
Entered words: aa bb cc
[aa, bb, cc]
------------------> 3 permutations for 1 words combination
Iterations: 3
0
1: aa
1
2: bb
2
3: cc
------------------> 6 permutations for 2 words combination
Iterations: 12
0
1
4: aabb
2
0
5: ccaa
1
2
6: bbcc
0
1
7: aabb
2
0
8: ccaa
1
2
9: bbcc
------------------> 9 permutations for 3 words combination
Iterations: 27
0
1
2
10: aabbcc
0
1
2
11: aabbcc
0
1
2
12: aabbcc
0
1
2
13: aabbcc
0
1
2
14: aabbcc
0
1
2
15: aabbcc
0
1
2
16: aabbcc
0
1
2
17: aabbcc
0
1
2
18: aabbcc