import java.util.*; public class Solution { public ArrayList<String> Permutation(String str) { ArrayList<String> ret = new ArrayList<>() ; if(str == null) { return ret ; } HashSet<String> set = new HashSet<>() ; recursion(str.toCharArray() , 0 , set , new StringBuilder()) ; for(String s : set) { ret.add(s) ; } return ret ; } public void recursion(char[] arr , int now ,HashSet<String> ret , StringBuilder path) { if(now == arr.length) { ret.add(new String(path)) ; return ; } for(int i = now ; i < arr.length ; i ++) { path.append(arr[i]) ; swap(arr,now , i) ; recursion(arr,now+1,ret,path) ; swap(arr,now , i) ; path.deleteCharAt(path.length()-1) ; } } public void swap(char[] arr , int i , int j) { char temp = arr[i] ; arr[i] = arr[j] ; arr[j] = temp ; } }