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:
The goal is to find all possible letter combinations that can be formed from a given string of digits, where each digit corresponds to certain letters on a phone keypad. The brute force way is to explore every possible combination of letters by trying each one out.
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 []
letter_combinations = ['']
for digit in digits:
# Need to store the newly created combinations
new_combinations = []
for combination in letter_combinations:
for letter in digit_to_letters[digit]:
# Append letter to form new combination
new_combinations.append(combination + letter)
# Update existing combinations with new ones
letter_combinations = new_combinations
return letter_combinations
The optimal way to generate letter combinations from a phone number involves building combinations step-by-step. Instead of creating every combination at once, we'll grow the possible combinations with each new digit. This avoids unnecessary calculations and makes the solution much faster.
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'
}
combinations = ['']
for digit in digits:
# Expand combinations based on letters of current digit
new_combinations = []
for combination in combinations:
for letter in digit_to_letters[digit]:
new_combinations.append(combination + letter)
combinations = new_combinations
return combinations
Case | How to Handle |
---|---|
Null or empty input string | Return an empty list since no combinations can be formed. |
Input string contains characters other than digits 2-9 | Ignore invalid characters or throw an IllegalArgumentException based on problem constraints. |
Input string with a single digit | Return the letters corresponding to that digit as individual strings in a list. |
Input string with all identical digits (e.g., '222') | The algorithm should correctly generate all combinations of the same letters. |
Long input string (e.g., 8+ digits) | The solution scales exponentially with the length of the input, so time complexity must be considered and stated. |
Input string resulting in a very large number of combinations (close to memory limits) | Ensure the algorithm doesn't exceed memory limits, potentially leading to an OutOfMemoryError. |
Digit '1' is present in the input | Digit '1' should be skipped since it does not map to any letters. |
Digit '0' is present in the input | Digit '0' should be skipped since it does not map to any letters. |