Created
February 2, 2018 20:10
-
-
Save LionRoar/216ff4fb7444fb381e369a5578ac1cb4 to your computer and use it in GitHub Desktop.
NoRepeatsPlease
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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