Skip to content

Instantly share code, notes, and snippets.

@ericat
Created October 19, 2016 10:49
Show Gist options
  • Save ericat/440692fbca32f03b359b9c4e5af11c24 to your computer and use it in GitHub Desktop.
Save ericat/440692fbca32f03b359b9c4e5af11c24 to your computer and use it in GitHub Desktop.
'use strict'
var results = [];
function perm(list, memo) {
for(var i=0; i < list.length; i++) {
var current = list.splice(i,1)[0];
if(list.length === 0) {
var permutation = memo.concat(current);
results.push(permutation.join(''));
}
perm(list.slice(), memo.concat(current));
list.splice(i, 0, current);
}
return results;
};
var example = "tacocat";
var list = example.split('');
console.log(perm(list, []));
//console.log(perm(['c', 'i'], ['a']));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment