So, I had this code to generate permutations of a word, and store it to a HashSet for later comparing with a dictionary. But when the input word has 10 or more letters, the permutation process becomes ridiculously slow. Besides using permutation algorithm, is there any ways to improve the performance of this process?
/**
* Returns a HashSet of the string permutation.
*
* @param prefix an empty string.
* @param str the string to create perm.
* @return permutations a HashSet of the permutation.
*/
private static HashSet<String> permutation(String prefix, String str) {
HashSet<String> permutations = new HashSet<String>();
int n = str.length();
if (n == 0) {
permutations.add(prefix);
} else {
for (int i = 0; i < n; i++) {
permutations.addAll(permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n)));
}
}
return permutations;
}