Simple javascript solution


  • 0
    I
    /**
     * @param {string} digits
     * @return {string[]}
     */
    var letterCombinations = function(digits) {
        if(!digits||digits.length==0) return [];
    
        var arr=[,,['a','b','c'],['d','e','f'],['g','h','i'],['j','k','l'],['m','n','o'],['p','q','r','s'],['t','u','v'],['w','x','y','z']];
        var result=[];
        for(var i=0;i<digits.length;i++){
            if(result.length==0){
                result=arr[+digits[i]];
            }else{
                result=map(result,arr[+digits[i]]);
            }
        }
        return result;
    };
    function map (result,arr) {
        var tmp=[];
        arr.forEach(function(e){
            tmp=tmp.concat(result.map(function(item){
                return item+e;
            }));
        }); 
        return tmp;
    }

Log in to reply
 

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