Last active
October 18, 2017 10:08
-
-
Save jebrial/c1e92b75bf7573a1eb04fbf6092d7bab to your computer and use it in GitHub Desktop.
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
const numMap = { | |
'1': [], | |
'2': ['a', 'b', 'c'], | |
'3': ['d', 'e', 'f'], | |
'4': ['g', 'h', 'i'], | |
'6': ['m', 'n', 'o'], | |
'7': ['p','q','r','s'] | |
}; | |
function possibleStrings(numPadStr) { | |
if(numPadStr.length === 0) { | |
return []; | |
} | |
if(numPadStr.length === 1) { | |
return numMap[numPadStr]; | |
} | |
const letters = []; | |
const nums = numPadStr.split(''); | |
nums.forEach((val) => { | |
letters.push(numMap[val]); | |
}); | |
return buildStringPossibilities(letters.shift(), letters); | |
} | |
function buildStringPossibilities(left, right) { | |
if (right.length === 0) { | |
return left; | |
} | |
const rightToMove = right.shift(); | |
const newLeft = []; | |
for(let i = 0; i < left.length; i++) { | |
for(let j = 0; j < rightToMove.length; j++) { | |
newLeft.push(left[i]+rightToMove[j]); | |
} | |
} | |
return buildStringPossibilities(newLeft, right); | |
} | |
console.log(possibleStrings('237')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment