LeetCode: Letter Combinations Of A Phone Number Solution

1/**
2 * @param {string} digits
3 * @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 }
16
17 const res = []
18
19 const recursion = (message, digitIndex) => {
20 if (digitIndex === digits.length) {
21 res.push(message)
22 return
23 }
24
25 for (const c of digitChars[digits[digitIndex]]) {
26 recursion(message + c, digitIndex + 1)
27 }
28 }
29
30 recursion("", 0)
31
32 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.

Next Post

LeetCode: Verifying An Alien Dictionary

Apr 9, 2021

HoningJS

Search Posts