Skip to content

Instantly share code, notes, and snippets.

@LionRoar
Created February 2, 2018 20:10
Show Gist options
  • Save LionRoar/216ff4fb7444fb381e369a5578ac1cb4 to your computer and use it in GitHub Desktop.
Save LionRoar/216ff4fb7444fb381e369a5578ac1cb4 to your computer and use it in GitHub Desktop.
NoRepeatsPlease
/*
Backtaking - BruteForce aproache
*/
function permAlone(str) {
var perms = [];
var arr = str.split('');
function nonRepeated(){
var regex = /(.)\1+/;
return perms.filter((p)=>(!p.match(regex)));
}
function permALoneHelper(a,chosen){
if(a.length==0){
perms.push(chosen);
}else{//choose/explore/unchoose
//(1-letter)
for(var i = 0; i < a.length;i++){
//choose the letter
var c = a[i];
chosen += c;
a.splice(i,1);
//explore what's after
permALoneHelper(a,chosen);
//un-choose
a.splice(i,0,c);
chosen = chosen.substring(0,chosen.length-1);
}
}
}
permALoneHelper(arr,"");
return nonRepeated().length;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment