Skip to content

Instantly share code, notes, and snippets.

@amitayh
Created February 10, 2015 14:06
Show Gist options
  • Save amitayh/3b12bcd82ff6505828b9 to your computer and use it in GitHub Desktop.
Save amitayh/3b12bcd82ff6505828b9 to your computer and use it in GitHub Desktop.
ES6 permutations calculator
function * permutations(str) {
var length = str.length;
if (length <= 1) {
yield str;
} else {
for (var i = 0; i < length; i++) {
var ch = str[i], substr = str.substr(0, i) + str.substr(i + 1);
for (var permutation of permutations(substr)) {
yield ch + permutation;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment