Skip to content

Instantly share code, notes, and snippets.

@jasonwaters
Last active March 10, 2017 18:55
Show Gist options
  • Save jasonwaters/5c818d4455b512058586a3f0fd0be5aa to your computer and use it in GitHub Desktop.
Save jasonwaters/5c818d4455b512058586a3f0fd0be5aa to your computer and use it in GitHub Desktop.
String Permutations
function permute(arr, permutation=[], permutations=[]) {
if(arr.length === 0) {
permutations.push(permutation);
}else {
arr.forEach(item => {
permute(arr.filter(value => value != item), permutation.concat(item), permutations);
});
}
return permutations;
}
console.log(permute('abc'.split('')));
//abc
//acb
//bac
//bca
//cab
//cba
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment