import java.util.*; public class Solution { public ArrayList<String> Permutation(String str) { ArrayList<String> res = new ArrayList<>(); dfs(0, str.length(), str.toCharArray(), new boolean[str.length()], new StringBuilder(), res); return res; } public void dfs(int k, int n, char[] cc, boolean[] visited, StringBuilder sb, ArrayList<String> res){ if(k == n){ if(!res.contains(sb.toString())){ res.add(sb.toString()); } return; } for(int i = 0; i < n; ++i){ if(!visited[i]){ sb.append(cc[i]); visited[i] = true; dfs(k + 1, n, cc, visited, sb, res); visited[i] = false; sb.deleteCharAt(sb.length() - 1); } } } }