17. Letter Combination of a Phone Number¶
Edited by Charles
Medium · Python · Backtrack
Problem
Read the original problem on LeetCode.
The official page contains the problem statement, examples, and constraints.
Explanation, examples & other approaches
Core idea
Recall 77. Combinations for the core idea. The trick part in the solution is to initiate a list of letters: parse empty strings to 0 and 1, so that the rest index correspond to letters shown on the keys.
Original study notes follow, including intermediate snippets and earlier attempts.
Note:
- Recall 77. Combinations for the core idea.
- The trick part in the solution is to initiate a list of letters: parse empty strings to 0 and 1, so that the rest index correspond to letters shown on the keys.
Study record
| Field | Value |
|---|---|
| Attempts | 1 |
| Mastery | ❚ |
| The Latest Attempt | January 21, 2026 |
Python solution¶
class Solution:
def __init__(self):
self.lettermap = [
"", # 0
"", # 1
"abc", # 2
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz"
]
def letterCombinations(self, digits: str) -> List[str]:
result = []
if len(digits) == 0:
return result
self.backtrack(digits, 0, [], result)
return result
def backtrack(self, digits, startIndex, path, result):
if startIndex == len(digits):
result.append(''.join(path))
return
digit = int(digits[startIndex])
letters = self.lettermap[digit]
for letter in letters:
path.append(letter)
self.backtrack(digits, startIndex + 1, path, result)
path.pop()