package class17; import java.util.ArrayList; import java.util.List; public class Code04_PrintAllPermutations { public static List permutation1(String s) { List ans = new ArrayList<>(); if (s == null || s.length() == 0) { return ans; } char[] str = s.toCharArray(); ArrayList rest = new ArrayList(); for (char cha : str) { rest.add(cha); } String path = ""; f(rest, path, ans); return ans; } public static void f(ArrayList rest, String path, List ans) { if (rest.isEmpty()) { ans.add(path); } else { int N = rest.size(); for (int i = 0; i < N; i++) { char cur = rest.get(i); rest.remove(i); f(rest, path + cur, ans); rest.add(i, cur); } } } public static List permutation2(String s) { List ans = new ArrayList<>(); if (s == null || s.length() == 0) { return ans; } char[] str = s.toCharArray(); g1(str, 0, ans); return ans; } public static void g1(char[] str, int index, List ans) { if (index == str.length) { ans.add(String.valueOf(str)); } else { for (int i = index; i < str.length; i++) { swap(str, index, i); g1(str, index + 1, ans); swap(str, index, i); } } } public static List permutation3(String s) { List ans = new ArrayList<>(); if (s == null || s.length() == 0) { return ans; } char[] str = s.toCharArray(); g2(str, 0, ans); return ans; } public static void g2(char[] str, int index, List ans) { if (index == str.length) { ans.add(String.valueOf(str)); } else { boolean[] visited = new boolean[256]; for (int i = index; i < str.length; i++) { if (!visited[str[i]]) { visited[str[i]] = true; swap(str, index, i); g2(str, index + 1, ans); swap(str, index, i); } } } } public static void swap(char[] chs, int i, int j) { char tmp = chs[i]; chs[i] = chs[j]; chs[j] = tmp; } public static void main(String[] args) { String s = "acc"; List ans1 = permutation1(s); for (String str : ans1) { System.out.println(str); } System.out.println("======="); List ans2 = permutation2(s); for (String str : ans2) { System.out.println(str); } System.out.println("======="); List ans3 = permutation3(s); for (String str : ans3) { System.out.println(str); } } }