Straight recursive Javascript Solution


  • 1
    R

    Relatively straight forward Javascript solution. Feedback 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 = numbers[digits[0]];
          
        for (let i = 0; i < currentNumber.length; i++) {
          let currentLetter = currentNumber[i];
          letterCombinations(digits.slice(1), current.concat(currentLetter), res);
        }
        
        return res;
    };

Log in to reply
 

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