Guest User

Untitled

a guest
Oct 18th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. const numMap = {
  2. '1': [],
  3. '2': ['a', 'b', 'c'],
  4. '3': ['d', 'e', 'f'],
  5. '4': ['g', 'h', 'i'],
  6. '6': ['m', 'n', 'o'],
  7. '7': ['p','q','r','s']
  8. };
  9.  
  10. function possibleStrings(numPadStr) {
  11.  
  12. if(numPadStr.length === 0) {
  13. return [];
  14. }
  15.  
  16. if(numPadStr.length === 1) {
  17. return numMap[numPadStr];
  18. }
  19.  
  20. const letters = [];
  21. const nums = numPadStr.split('');
  22.  
  23. nums.forEach((val) => {
  24. letters.push(numMap[val]);
  25. });
  26.  
  27. return buildStringPossibilities(letters.shift(), letters);
  28. }
  29.  
  30. function buildStringPossibilities(left, right) {
  31. if (right.length === 0) {
  32. return left;
  33. }
  34. const rightToMove = right.shift();
  35. const newLeft = [];
  36. for(let i = 0; i < left.length; i++) {
  37. for(let j = 0; j < rightToMove.length; j++) {
  38. newLeft.push(left[i]+rightToMove[j]);
  39. }
  40. }
  41. return buildStringPossibilities(newLeft, right);
  42. }
  43. console.log(possibleStrings('237'));
Add Comment
Please, Sign In to add comment