#17
Letter Combinations of a Phone Number
medium· Backtrackingruns: 0Given a string containing digits from 2 to 9 inclusive, return all possible letter combinations that the number could represent. A standard mapping of digits to letters (like a phone keypad) is used; 1 does not map to any letters.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 700
class Solution:
def letterCombinations(self, digits: str) -> list[str]:
if not digits:
return []
mapping = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz",
}
result = []
path = []
def backtrack(i: int) -> None:
if i == len(digits):
result.append("".join(path))
return
for c in mapping[digits[i]]:
path.append(c)
backtrack(i + 1)
path.pop()
backtrack(0)
return result
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.