Created
June 21, 2017 19:39
-
-
Save theptrk/686261ba7828e8946c674b772993f842 to your computer and use it in GitHub Desktop.
given a telephone number, return a list of possible mnemonics using a standard US telephone
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
const phoneNumberMnemonics = (phoneNumber) => { | |
const letters = ["0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"] | |
const numbers = phoneNumber.split(''); | |
const results = []; | |
const wipStack = [ | |
{ wip: '', remaining: numbers } | |
]; | |
while (wipStack.length > 0) { | |
let [wip, remainingNumbers] = wipStack.pop(); | |
if (remainingNumbers.length > 0) { | |
let number = remainingNumbers.pop(); | |
letters[number].forEach((letter) => { | |
wipStack.push({ | |
wip: wip + letter, | |
remaining: remainingNumbers.slice(); | |
}) | |
}); | |
} else { | |
results.push(wip); | |
} | |
} | |
return results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment