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 digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Example 1:
Input: digits = "23" Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
Example 2:
Input: digits = "" Output: []
Example 3:
Input: digits = "2" Output: ["a","b","c"]
Constraints:
0 <= digits.length <= 4
digits[i]
is a digit in the range ['2', '9']
.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We want to find all possible letter combinations for a given phone number's digits. The brute force approach explores every single combination until we've covered them all. It's like trying every single password until we guess the right one.
Here's how the algorithm would work step-by-step:
def letter_combinations_brute_force(digits):
digit_to_letters = {
'2': 'abc',
'3': 'def',
'4': 'ghi',
'5': 'jkl',
'6': 'mno',
'7': 'pqrs',
'8': 'tuv',
'9': 'wxyz'
}
if not digits:
return []
combinations = ['']
for digit in digits:
new_combinations = []
# Iterate through existing combinations
for combination in combinations:
# Iterate through letters of the current digit
for letter in digit_to_letters[digit]:
new_combinations.append(combination + letter)
# This is to update to all possible combinations so far.
combinations = new_combinations
return combinations
The best way to find all letter combinations is to build them step-by-step, starting with the first digit and adding letters from each subsequent digit. This avoids generating useless combinations and focuses only on valid possibilities. It's like building a tree of choices, where each branch represents a possible letter.
Here's how the algorithm would work step-by-step:
def letterCombinations(digits):
if not digits:
return []
digit_to_letters = {
'2': 'abc',
'3': 'def',
'4': 'ghi',
'5': 'jkl',
'6': 'mno',
'7': 'pqrs',
'8': 'tuv',
'9': 'wxyz'
}
result_combinations = ['']
for digit in digits:
new_combinations = []
# Iterate over each existing combination
for combination in result_combinations:
# Create new combinations by adding letters.
for letter in digit_to_letters[digit]:
new_combinations.append(combination + letter)
# Update combinations for next digit.
result_combinations = new_combinations
return result_combinations
Case | How to Handle |
---|---|
Null or empty input string | Return an empty list as there are no digits to generate combinations from. |
Input string contains characters other than digits 2-9 | Ignore the invalid characters or throw an exception, depending on requirements; the provided solution implicitly ignores by lack of mapping. |
Input string contains a single digit | Return a list containing all letters associated with that digit. |
Input string with a large number of digits (e.g., >10) | Ensure the algorithm scales efficiently, potentially using an iterative approach to avoid stack overflow issues from recursion. |
Input consists of the same digit repeated multiple times (e.g., '222') | The algorithm should correctly generate all combinations, including those with repeated letters. |
Combination length exceeds available memory | This should be tested against large inputs, and memory usage optimized; a streaming approach might be necessary if generating all combinations is infeasible. |
Digits '0' or '1' in input. | These digits have no letter mapping and should be ignored or cause an exception depending on problem constraints. |
Input '7777' generates an exponential number of combinations due to 4 letters mapped to '7'. | Be aware of exponential runtime and memory consumption, and consider imposing a limit on the input length. |