final class Combination {
public static void findCombinations(String[][] sets) {
int combinations = 1;
for(int i = 0; i < sets.length; combinations *= sets[i].length, i++);
for(int i = 0; i < combinations; i++) {
int j = 1;
for(String[] set : sets) {
System.out.print(set[(i/j)%set.length] + " ");
j *= set.length;
}
System.out.println();
}
}
public static void main(String[] args) {
findCombinations(new String[][]{{"a","b","c"}, {"d","e","i"}, {"f","g","h"}});
}
}
My answer looks like this
a d f
b d f
c d f
a e f
b e f
c e f
a i f
b i f
c i f
a d g
b d g
c d g
a e g
b e g
c e g
a i g
b i g
c i g
a d h
b d h
c d h
a e h
b e h
c e h
a i h
b i h
c i h
I want to know the time complexity of my solution and if there are any ways to improve the solution.
combinations *= sets[i].lengthinto the body of the loop, rather than having it in the head.