Skip to content

Instantly share code, notes, and snippets.

@theptrk
Created June 21, 2017 19:39
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 theptrk/686261ba7828e8946c674b772993f842 to your computer and use it in GitHub Desktop.
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
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