class Solution {
public String[] permutation(String S) {
int length = 1;
for(int i = 1;i <= S.length();i++){
length *= i;
}
LinkedList<String> result = new LinkedList<>();
backcheck(S.toCharArray(),0,result);
return result.toArray(new String[0]);
}
public void backcheck(char[] nums,int index,LinkedList<String> result){
if(index == nums.length){
result.add(new String(nums));
}else{
HashSet<Character> set = new HashSet<>();
for(int i = index;i < nums.length;i++){
if(!set.contains(nums[i])){
set.add(nums[i]);
swap(nums,index,i);
backcheck(nums,index + 1,result);
swap(nums,index,i);
}
}
}
}
public void swap(char nums[],int i,int j){
if(i != j){
char temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
|