Created
December 10, 2015 02:05
-
-
Save NameFILIP/e6dbe82580aa51783ce7 to your computer and use it in GitHub Desktop.
Password Permutations
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
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