和全排列一样,只不过由数组变为了字符串
可以同样用数组path存储,也可以采用字符串存储
数组存储:res.push(path.join(''))
字符串存储:
let path = ''
path += str[i]
path = path.slice(0,path.length-1)
function Permutation(str)
{
let used = [];
let path = [];
let res = [];
function traceTrack(){
if(path.length == str.length){
res.push(path.join(''));
return;
}
for(let i=0; i<str.length; i++){
if(i!=0 && str[i]==str[i-1] && !used[i-1])
continue;
if(!used[i]){
used[i] = true;
path.push(str[i]);
traceTrack();
path.pop();
used[i] = false;
}
}
}
traceTrack();
return res;
}