I have permutation methods
public void permute(String str) {
permute(str.toCharArray(), 0, str.length() - 1);
}
private void permute(char[] str, int low, int high) {
if (low == high) {
writeIntoSet(new String(str, 0, length));
} else {
for (int i = low; i <= high; i++) {
char[] x = charArrayWithSwappedChars(str, low, i);
permute(x, low + 1, high);
}
}
}
private char[] charArrayWithSwappedChars(char[] str, int a, int b) {
char[] array = str.clone();
char c = array[a];
array[a] = array[b];
array[b] = c;
return array;
}
But when I put string, which is 10 letters length into this method, it makes 10! combinations and it takes so much time. Is there any possibility how to make it faster?
EDIT
I need to make permutations from 10 letters, but after that, I search these "words" in dictionary. For example I have - CxRjAkiSvH and I need words CAR, CARS, CRASH etc. Is there any performance option?
There are existing algorithms for generating permutations which may be slightly more efficient than the one you're using, so you could take a look at one of those. I've used the Johnson-Trotter algorithm in the past, which gets a slight speed up by making the minimum change possible to get the next permutation each time. I don't know what kind of constraints you have to work within, but if you don't have to use Java it might be better not to. It simply won't be the fastest for this. Especially if your algorithm is using recursion. As someone else has suggested, if you're sticking with this algorithm you might be best served to move away from the recursive approach and try using a loop.