Enter a string and print out all permutations of the characters in the string.
You can return this array of strings in any order, but no repeating elements.
The sample
Input: s = "ABC" output: [" ABC ", "acb", "America", "bca", "cab", "cba"]Copy the code
var res=[]; var str=''; var permutation = function(s) { var res=[]; var newstr=''; var arr=s.split(''); var dfs=function(arr,newstr){ if(arr.length===1){ res.push(newstr + arr[0]) } else{ for(var i=0; i<arr.length; i++){ let char=arr.splice(i,1).join(""); newstr+=char; dfs(arr,newstr); arr.splice(i,0,char); newstr=newstr.slice(0,-1); } } } dfs(arr,newstr); return [...new Set(res)]; }; var arr=''; console.log(permutation(arr));Copy the code