Created
February 10, 2015 14:06
-
-
Save amitayh/3b12bcd82ff6505828b9 to your computer and use it in GitHub Desktop.
ES6 permutations calculator
This file contains hidden or 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
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