Recursive JS solution


  • 0
    J
    var letterCombinations = function(digits) {
        if (!digits.length) {return []}
        var mapping = [
            [],[],["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 makeStrings = function(strings, digits) {
            if (!digits.length) {return strings;}
            var newStrings = [];
            var thisDigit = digits[0];
            strings.forEach(function(s) {
                mapping[thisDigit].forEach(function(l) {
                    newStrings.push(s + l);
                })
            })
            return makeStrings(newStrings, digits.slice(1));
        }
        
        return makeStrings([""], digits);
    };

Log in to reply
 

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