LeetCode: Letter Combinations Of A Phone Number Solution
1/**2 * @param {string} digits3 * @return {string[]}4 */5var letterCombinations = function (digits) {6 const digitChars = {7 2: "abc",8 3: "def",9 4: "ghi",10 5: "jkl",11 6: "mno",12 7: "pqrs",13 8: "tuv",14 9: "wxyz",15 }1617 const res = []1819 const recursion = (message, digitIndex) => {20 if (digitIndex === digits.length) {21 res.push(message)22 return23 }2425 for (const c of digitChars[digits[digitIndex]]) {26 recursion(message + c, digitIndex + 1)27 }28 }2930 recursion("", 0)3132 return res.filter(Boolean)33}
Comments
Loading comments...
Tags
leetcode
string
backtracking
dfs
recursion
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.