You are given a string digits made up of digits from 2 through 9 inclusive.
Each digit (not including 1) is mapped to a set of characters as shown below:
A digit could represent any one of the characters it maps to.
Return all possible letter combinations that digits could represent. You may return the answer in any order.
Example 1:
Input: digits = "34"
Output: ["dg","dh","di","eg","eh","ei","fg","fh","fi"]Example 2:
Input: digits = ""
Output: []Constraints:
0 <= digits.length <= 42 <= digits[i] <= 9
You should aim for a solution with O(n * (4^n)) time and O(n) space, where n is the length of the input string.
We can use a hash map to pair all the digits with their corresponding letters. Think of this as a decision tree, where at each step, we have a digit, and we select one of multiple characters to proceed to the next digit in the given string digits. Can you think of an algorithm to generate all combinations of strings?
We can use backtracking where we select a character and process it, then backtrack to process another character. We recursively iterate on the given string with index i. At each step, we consider the letters from the hash map that correspond to the digit at the i-th index. Can you think of the base condition to stop this recursive path?
We initialize an empty string that represents the choices of the characters throughout the current recursive path. When the index i reaches the end of the string, we add the current string to the result list and return.
Before attempting this problem, you should be comfortable with:
Each digit maps to a set of characters (like on a phone keypad).
The task is to choose one character per digit, in order, and generate all possible combinations.
Think of it as building a string step by step:
i, pick one character from the mapping of digits[i]This is a classic decision tree problem:
Backtracking lets us explore all branches efficiently.
2-9) to their corresponding letters.backtrack(index, currentString):currentString length equals the number of digits:digits[index]:currentString0 with an empty string.class Solution:
def letterCombinations(self, digits: str) -> List[str]:
res = []
digitToChar = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "qprs",
"8": "tuv",
"9": "wxyz",
}
def backtrack(i, curStr):
if len(curStr) == len(digits):
res.append(curStr)
return
for c in digitToChar[digits[i]]:
backtrack(i + 1, curStr + c)
if digits:
backtrack(0, "")
return resInstead of using recursion, we build combinations level by level.
Start with an empty string.
For each digit:
This is similar to BFS / level-wise expansion:
[""].class Solution:
def letterCombinations(self, digits: str) -> List[str]:
if not digits:
return []
res = [""]
digitToChar = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "qprs",
"8": "tuv",
"9": "wxyz",
}
for digit in digits:
tmp = []
for curStr in res:
for c in digitToChar[digit]:
tmp.append(curStr + c)
res = tmp
return resWhen digits is empty, returning [""] (list with empty string) is wrong. The correct answer is [] (empty list) since there are no combinations to form.
# Wrong: returns list with empty string
if not digits:
return [""]
# Correct: return empty list
if not digits:
return []Digit 7 maps to "pqrs" (4 letters) and digit 9 maps to "wxyz" (4 letters), not 3 letters each. Using "qprs" instead of "pqrs" for digit 7 produces wrong orderings.
When using an array for digit mapping, digits 0 and 1 have no letters. Forgetting this offset or incorrectly computing digit - '0' leads to index errors or wrong character mappings.
// digitToChar[0] and digitToChar[1] should be empty
String[] digitToChar = {"", "", "abc", "def", ...};
// Access with: digitToChar[digit - '0']