Skip to content

Instantly share code, notes, and snippets.

@NameFILIP
Created December 10, 2015 02:05
Show Gist options
  • Save NameFILIP/e6dbe82580aa51783ce7 to your computer and use it in GitHub Desktop.
Save NameFILIP/e6dbe82580aa51783ce7 to your computer and use it in GitHub Desktop.
Password Permutations
var replacements = {
p: ['P', '1'],
a: ['A', '@'],
b: ['B'],
s: ['S', '$'],
o: ['O', '0'],
w: ['W']
};
function permutations(state, word, index) {
if (index >= word.length) {
return state;
}
var letter = word[index];
var alternatives = replacements[letter] || [];
// clone array for immutability
alternatives = alternatives.slice();
alternatives.push(letter)
var newState;
if (state.length === 0) {
newState = alternatives;
} else {
newState = [];
state.forEach(function(partial) {
alternatives.forEach(function(alternative) {
newState.push(partial + alternative);
});
});
}
return permutations(newState, word, index + 1);
}
var res = permutations([], 'password', 0);
console.log(res);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment