Javascript Solution using reduce


  • 0
    M
        if(digits !=''){
            let mapping = {
                  '2': ['a', 'b', 'c'],
                  '3': ['d', 'e', 'f'],  
                  '4': ['g', 'h', 'i'],  
                  '5': ['j', 'k', 'l'],  
                  '6': ['m', 'n', 'o'],  
                  '7': ['p', 'q', 'r', 's'],  
                  '8': ['t', 'u', 'v'],  
                  '9': ['w', 'x', 'y', 'z']
            }
            let digitsArr = [];
              digitsArr =  digits.split('').map(function(num){
                    return mapping[num];
                });
            let output =  digitsArr.reduce(function(a,b){
                   let result = []; 
                   for(i=0;i<a.length;i++){
                       for(j=0;j<b.length;j++){
                           result.push(a[i]+b[j]);
    
                       }
                   }
                     return result;
                });
            return output;
        }else{
            return [];
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.