I´m looking for a algorithm which returns me a list of all possible combinations of x-characters and a defined length.
For example when I have the characters a and b and the length two I would like to get this output:
a
b
aa
ab
ba
bb
But I also want the code to be resumable, so that I can save the current combination/position and resume at exactly the same combination/position.
This was my first try, it works fine but it is not resumable or at least I have no idea how to do it:
public class test {
static char[] chars = "ab".toCharArray();
static int maxLength = 2;
public static void main(String[] args) {
nextString("");
}
private static void nextString(String s) {
int i = 0;
while (i < chars.length) {
String snew;
snew = s + new Character(chars[i]).toString();
System.out.println(snew);
if (snew.length() <= maxLength - 1)
nextString(snew);
i++;
}
}
}