用回溯思想做,结果用Set去重
import java.util.*; public class Solution { Set<String> res = new HashSet<>(); public ArrayList<String> Permutation(String str) { boolean[] visited = new boolean[str.length()]; recursion(str,"",visited); ArrayList<String> list = new ArrayList<>(res); Collections.sort(list); return list; } public void recursion(String str,String current,boolean[] visited){ if(str.length()==current.length()){ res.add(current); return; } for(int i = 0;i<str.length();i++) { if(!visited[i]){ current+=str.charAt(i); visited[i] = true; recursion(str,current,visited); current = current.substring(0,current.length()-1); visited[i] = false; } } } }