Javascript Recursive Approach with Hash Map


  • 1
    R

    Tips and suggestions always appreciated

    const letterCombinations = (digits, current = "", res = []) => {
        const numbers = {
            2: "abc",
            3: "def",
            4: "ghi",
            5: "jkl",
            6: "mno",
            7: "pqrs",
            8: "tuv",
            9: "wxyz"
        }
        
        if (digits.length === 0 && current === "") {
            return res;
        }
      
        if (digits.length < 1) {
            return(res.push(current));
        }
      
        let currentNumber,
            currentLetter;
      
        currentNumber = numbers[digits[0]];
      
        for (let i = 0; i < currentNumber.length; i++) {
            currentLetter = numbers[digits[0]][i];
            letterCombinations(digits.slice(1), current + currentLetter, res);
        }
      
        return res;
    }

Log in to reply
 

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