Another approach is to interpret your charset as digits of a numbersystem and your desired output as all n digit numbers. For example, in decimal system we have the digits 0 to 9 and all two-digit numbers (with leading zero) would be the numbers
00 - 99
three-digit
000 - 999
and so on. In octal system, however, your numbers would go from
00 - 77
or from
000 - 777
and in binary
00 - 11
or
000 - 111
If we now replace in your charset the letters with digits, i.e. a
with 0
, b
with 1
, c
with 2
...
aaa
in your output is the same as 000
and fff
the same as 555
. That means the task is to create all two-digit, three-digit, ...., 5-digit numbers in the base six number system (charset length) and convert the digits back to the letters from the given charset. A starting point for such an algorithm could be something like:
public static void main(String[] args) {
String str = "abcdef";
//create a map which looks like {0=a, 1=b, 2=c, 3=d, 4=e, 5=f}
Map<Character,Character> map = new HashMap<>();
for(int i = 0; i <str.length(); i ++){
map.put((char)(i+'0'), str.charAt(i));
}
//convert numbers to string using //Integer.toString(int i, int radix)
//use String#format & String#replace to have a string representation with leading zeros
for(int n = 2; n <= 5; n++){
int maxValue = (int)Math.pow(str.length(), n);
for(int i = 0; i < maxValue; i++){
String temp = String.format("%"+n+"s", Integer.toString(i, str.length())).replace(' ', '0');
for(char c: map.keySet()){
temp = temp.replace(c, map.get(c));
}
System.out.println(temp);
}
}
}