I know the runtime for generating all permutations is supposed to be O(n!)\$O(n!)\$. I'm not sure if the code I wrote for it runs in O(n!)\$O(n!)\$ though. I'm having trouble analyzing its runtime.
The for(char c: cArray) part is in O(n)\$O(n)\$. What about for (String s: currResults) and for (int i = 0; i < s.length() + 1; i++)?
public static Set<String> getPermutations(String str) {
char[] cArray = str.toCharArray();
Set<String> realRes = new HashSet<String>();
Set<String> results = new HashSet<String>();
for (char c : cArray) {
Set<String> currResults = new HashSet<String>(results);
String charToAdd = String.valueOf(c);
results.add(charToAdd);
for (String s : currResults) {
for (int i = 0; i < s.length() + 1; i++) {
String newWord = s.substring(0, i) + charToAdd + s.substring(i);
if (newWord.length() == str.length()) {
realRes.add(newWord);
}
else {
results.add(newWord);
}
}
}
}
return realRes;
}