1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public static void f1(char[] s, int i, StringBuilder path, HashSet<string> set){ if(i == s.length){ set.add(path.toString();) } else{ path.append(s[i]); f1(s, i + 1, path, set); path.deleteCharAt(path.length()-1); f1(s, i+1, path, set); } }
public static String[] generatePermutation1(String str){ char[] s = str.toCharArray(); HashSet<String> set = new HashSet<>(); f1(s, 0, new StringBuilder(), set); int m = set.size(); String[] ans = new String[m]; int i = 0; for(String cur : set){ ans[i++] = cur; } return ans; }
|