Skip to content

Instantly share code, notes, and snippets.

@Daniel-Hug
Last active June 15, 2017 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Daniel-Hug/86e25f7b36333f39be69aa2d9f4d6040 to your computer and use it in GitHub Desktop.
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
// 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