输入一个字符串,打印出该字符串中字符的所有排列。
你可以以任意顺序返回这个字符串数组,但里面不能有重复元素。
示例:
输入:s = "abc"
输出:["abc","acb","bac","bca","cab","cba"]
限制:
1 <= s 的长度 <= 8
代码:
class Solution {
public String[] permutation(String s) {
List<String> result = new ArrayList<>();
boolean visited[] = new boolean[s.length()];
char arr[] = s.toCharArray();
Arrays.sort(arr);
StringBuilder x = new StringBuilder();
helper(result,visited,arr,x);
String res[] = new String[result.size()];
for(int i=0;i<result.size();i++){
res[i] = result.get(i);
}
return res;
}
public void helper(List<String> result,boolean visited[],char arr[],StringBuilder x){
if(x.length()==arr.length){
result.add(x.toString());
return;
}
for(int i=0;i<arr.length;i++){
if(i!=0&&arr[i]==arr[i-1]&&visited[i-1]) continue;
if(!visited[i]){
x.append(arr[i]);
visited[i] = true;
helper(result,visited,arr,x);
x.deleteCharAt(x.length()-1);
visited[i] = false;
}
}
}
}