Skip to content

Instantly share code, notes, and snippets.

@pfiller
Last active August 29, 2015 14:10
Show Gist options
  • Save pfiller/2d2905b5dad6bddb1235 to your computer and use it in GitHub Desktop.
Save pfiller/2d2905b5dad6bddb1235 to your computer and use it in GitHub Desktop.
Function to return all permutations of a string
function showAllPermutations(mixyString){
var unMixy, permutations = [];
popAndLock = function(string, root){
var i;
if(root == null){
root = ""
}
for(i=0; i<string.length; i+=1){
startCharacter = string[i];
leftover = string.slice(0,i).concat(string.slice(i+1));
if(leftover.length > 1){
popAndLock(leftover, root+startCharacter);
}
else{
permutations.push(root+startCharacter+leftover);
}
}
}
unMixy = mixyString.split("").sort().join("");
popAndLock(unMixy);
return permutations;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment