Javascript fastest solution (100ms)


  • 0
    L
    var letterCombinations = function(digits) {
        if(digits.length === 0) return [];
        var obj = {
            '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']
        };
    
        return digits.split('').map(function(num){
            return [].concat(obj[num]);
        }).reduce(function(coomb, item){
            var l = coomb.length;
            coomb.forEach(function(coombination){
                item.forEach(function(letterOfItem){
                        coomb.push(coombination + letterOfItem);
                });
            });
            return coomb.slice(l);
        });
    };

Log in to reply
 

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