JavaScript DFS clean solution


  • 0
    var letterCombinations = function(digits) {
        if (digits === "") return [];
        var result = [];
        var array = [ "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" ];
        var digits = Array.from( digits );
        function dfs( str, idx ){
            if( str.length === digits.length ){
                result.push( str );
                return;
            }
            var candidates = Array.from(array[digits[idx]]);
            candidates.forEach(function(i){
                dfs( str + i, idx + 1 )
            })
        }
        dfs( "", 0 );
        return result;
    };

Log in to reply
 

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