Need help for this simple thing that is annoying me. I have seen many similar algorithms but I want to do this in exactly the stated way to reach ALL possible combinations / permutation in a given charset array.
lets take an example of a password cracker brute forcer
e.g char[] charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
?
stated way:
its like this. for the current example.
a,b,c,d......z then at last index "z".
it goes like
aa,ab,ac....az. then
ba,bb,bc,bd........bz then
same for ca, cb, and so on.
aaaa,aaab,aaac......aaaz then
baaa,baab,baac.......baaz to zzzzzzzzzzzzzzzzzzzzzzzzzz
Code I reached so far:
(well not a solution though) is to have as many for loops as the length of charset array. that is insane. this works ok. but i need intelligent one.
public class Bruteforcer {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
char[] charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
int currentIndex = 0;
String currentString = "";
for (int i = 0; i < charset.length; i++) {
char currentChar = charset[i];
for (int j = 0; j < charset.length; j++) {
char c = charset[j];
currentString = "" +currentChar + c;
System.out.println(currentString);
}
}
}
}