import java.util.*; public class Solution { public ArrayList<String> Permutation(String str) { ArrayList<String> res = new ArrayList<>() ; if(str == null || str.length() == 0) return res ; char[] arr = str.toCharArray() ; Arrays.sort(arr) ; help(arr , new boolean[arr.length] , new StringBuilder() , res) ; return res ; } //使用辅助数组,进行标记 public void help(char[] arr , boolean[] isV , StringBuilder sb ,ArrayList<String> res) { if(sb.length() == arr.length) { res.add(sb.toString()) ; return ; } for(int i = 0 ; i < arr.length ; i ++) { if(isV[i]) continue ; //处理重复的情况 if(i > 0 && arr[i] == arr[i - 1] && !isV[i - 1]) continue ; isV[i] = true ; sb.append(String.valueOf(arr[i])) ; help(arr , isV , sb , res) ; sb.deleteCharAt(sb.length() - 1) ; isV[i] = false ; } } }