Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Letter Combinations of a Phone Number in Python
The Letter Combinations of a Phone Number problem involves generating all possible letter combinations that a string of digits (2-9) could represent on a phone keypad. Each digit maps to a set of letters, similar to old telephone keypads.
Phone Keypad Mapping
| 1 | 2 abc |
3 def |
| 4 ghi |
5 jkl |
6 mno |
| 7 pqrs |
8 tuv |
9 wxyz |
| * | 0 | # |
For example, if the input is "23", the possible combinations are ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Algorithm Approach
We'll use backtracking to solve this problem:
- Create a mapping of digits to their corresponding letters
- Use recursion to build combinations character by character
- At each level, try all possible letters for the current digit
- When we've processed all digits, add the combination to results
Implementation
class Solution:
def letterCombinations(self, digits):
if len(digits) == 0:
return []
# Mapping of digits to letters
phone_map = {
'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl',
'6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'
}
result = []
self.backtrack(digits, phone_map, result, "", 0)
return result
def backtrack(self, digits, phone_map, result, current_string, current_level):
# Base case: we've processed all digits
if current_level == len(digits):
result.append(current_string)
return
# Get the current digit and its corresponding letters
current_digit = digits[current_level]
letters = phone_map[current_digit]
# Try each letter for the current digit
for letter in letters:
self.backtrack(digits, phone_map, result,
current_string + letter, current_level + 1)
# Test the solution
solution = Solution()
print(solution.letterCombinations("23"))
print(solution.letterCombinations(""))
print(solution.letterCombinations("2"))
['ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf'] [] ['a', 'b', 'c']
Alternative: Iterative Approach
Here's an iterative solution using a queue-like approach ?
def letterCombinations(digits):
if not digits:
return []
phone_map = {
'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl',
'6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'
}
combinations = ['']
for digit in digits:
new_combinations = []
for combination in combinations:
for letter in phone_map[digit]:
new_combinations.append(combination + letter)
combinations = new_combinations
return combinations
# Test the iterative solution
print(letterCombinations("23"))
print(letterCombinations("7"))
['ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf'] ['p', 'q', 'r', 's']
Time and Space Complexity
Time Complexity: O(3^N × 4^M) where N is the number of digits that map to 3 letters and M is the number of digits that map to 4 letters (like 7 and 9).
Space Complexity: O(3^N × 4^M) for storing all combinations, plus O(N) for the recursion stack.
Conclusion
The backtracking approach efficiently generates all letter combinations by exploring each possibility systematically. The iterative method offers an alternative that builds combinations level by level, both achieving the same result with different implementation styles.
