Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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'));
Add Comment
Please, Sign In to add comment