Javascript iterative solution


  • 1
    S
     * @param {string} digits
     * @return {string[]}
     */
    var letterCombinations = function(digits) {
        var array = ["abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"];
        var final = [];
        for(var i = 0; i < digits.length; i++){
            var curr = parseInt(digits.charAt(i));
            var letters = array[curr - 2];
            final = backtracking(final, letters);
        }
        return final;
    };
    
    var backtracking = function(final, curr){
        var tempfinal = [];
        if(final.length === 0){
           for(var q = 0; q < curr.length; q++){
               tempfinal.push(curr.charAt(q)); 
            } 
        }
        for(var j = 0; j < final.length; j++){
            for(var p = 0; p < curr.length; p++){
               tempfinal.push(final[j] + curr.charAt(p)); 
            }
        }
        return tempfinal;
    };

Log in to reply
 

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