Last active
June 15, 2017 19:28
-
-
Save Daniel-Hug/86e25f7b36333f39be69aa2d9f4d6040 to your computer and use it in GitHub Desktop.
JS function: get all word permutations of a given length over a given alphabet
This file contains 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
// get all words of a certain length in alphabet | |
function permute(alphabet, wordLength) { | |
if (wordLength === 0) return ['']; | |
var words = []; | |
var shorterWords = permute(alphabet, wordLength - 1); | |
for (var i = 0; i < alphabet.length; i++) { | |
for (var j = 0; j < shorterWords.length; j++) { | |
words.push(alphabet[i] + shorterWords[j]); | |
} | |
} | |
return words; | |
} | |
console.log(permute('ab', 4).join('\n')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment