We're going to work on a problem called "Letter Combinations of a Phone Number".
The problem is as follows: Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
2 -> "abc" 3 -> "def" 4 -> "ghi" 5 -> "jkl" 6 -> "mno" 7 -> "pqrs" 8 -> "tuv" 9 -> "wxyz"
So, you need to write a function that takes a string of digits as input and returns a list of strings, where each string is a possible letter combination. Here are some examples:
Example 1:
Example 2:
Example 3:
Can you describe your approach to solving this problem?
Let's break down the problem of generating letter combinations from a phone number. I'll start with a naive approach, then move to an optimized one, covering complexities, edge cases, and code.
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
2: abc 3: def 4: ghi 5: jkl 6: mno 7: pqrs 8: tuv 9: wxyz
A brute-force approach would involve generating all possible combinations by iterating through the letters corresponding to each digit and concatenating them. This would involve nested loops or recursion.
Let's say the input is "23". We would iterate through 'a', 'b', 'c' for '2' and for each of those, iterate through 'd', 'e', 'f' for '3'.
This is conceptually simple, but not always the most efficient, especially for longer digit strings.
The optimal solution typically involves a backtracking algorithm. Backtracking systematically explores all possible solutions by trying every potential candidate. If a candidate leads to a dead end, we backtrack (undo the last choice) and try a different candidate.
Here's the general algorithm:
backtrack(combination, next_digits)
) that takes the current combination and the remaining digits as input.next_digits
is empty), add the current combination
to the list of results.next_digits
.backtrack
with the updated combination
(adding the current letter) and the remaining digits (removing the first digit).python def letterCombinations(digits: str) -> list[str]: if not digits: return []
digit_to_letters = {
'2': 'abc',
'3': 'def',
'4': 'ghi',
'5': 'jkl',
'6': 'mno',
'7': 'pqrs',
'8': 'tuv',
'9': 'wxyz'
}
combinations = []
def backtrack(combination, next_digits):
if not next_digits:
combinations.append(combination)
return
digit = next_digits[0]
letters = digit_to_letters[digit]
for letter in letters:
backtrack(combination + letter, next_digits[1:])
backtrack("", digits)
return combinations
digit_to_letters
: A dictionary maps digits to their corresponding letters.combinations
: A list to store the resulting combinations.backtrack(combination, next_digits)
:
next_digits
is empty, it means we have processed all digits, so we add the current combination
to the combinations
list.next_digits
. We then iterate through each letter and recursively call backtrack
with the updated combination and the remaining digits.combinations
.For the input "23", the function would return: ['ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf']