My JavaScript solution with DFS


  • 0
    Z
    var letterCombinations = function(digits) {
        if (!digits) {
            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 len = digits.length;
        var ans = [];
        function dfs(str,pos){
            if (pos == len) {
                ans.push(str);
                return;
            }
            mapping[digits[pos]].forEach(function(v){
                dfs(str+v,pos+1);
            });
        }
        dfs('',0);
        return ans;
    };
    

Log in to reply
 

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