Letter Combinations of a Phone Number
Problem Statement:β
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:
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"]
β Solution: Backtrackingβ
class Solution {
public:
vector<string> result;
string charToDigit[10] = {
"", // 0 has no letters
"", // 1 has no letters
"abc", // 2
"def", // 3
"ghi", // 4
"jkl", // 5
"mno", // 6
"pqrs", // 7
"tuv", // 8
"wxyz" // 9
};
// Recursive function to build combinations
void f(string digits, int index, string ¤t){
if(index == digits.size()){
result.push_back(current); // full combination formed
return;
}
string letter = charToDigit[digits[index] - '0'];
for(char ch : letter){
current.push_back(ch); // choose
f(digits, index + 1, current); // explore
current.pop_back(); // backtrack
}
}
vector<string> letterCombinations(string digits) {
if(digits.empty()) return {}; // edge case: no input
string current;
f(digits, 0, current);
return result;
}
};
π Revision Notesβ
β Solution: Backtrackingβ
π How It Worksβ
- Each digit maps to some letters using a
charToDigitarray, just like on a phone keypad. - Start from the first digit, and for each possible letter:
- Add it to the current string.
- Recursively move to the next digit.
- When all digits are processed (
index == digits.size()), the string is added toresult.
- Uses backtracking to try all combinations and undo choices (with
pop_back()).
π§© Key Formula / Recurrenceβ
-
Recurrence is like a tree:
f(index) = for each letter in map[digits[index]]:
f(index + 1) -
At each level, you explore all mapped letters.
β±οΈ Time & Space Complexityβ
| Metric | Complexity |
|---|---|
| Time | O(3βΏ Γ 4α΅) β where n is the number of digits mapping to 3 letters, m to 4 letters |
| Space | O(k) for recursion stack, O(3βΏ Γ 4α΅) for storing answers |
β οΈ Edge Casesβ
- Empty string (
"") β return[] - Digits like
0or1β safely ignored due to""mapping
π‘ Other Approachesβ
| Approach | Time Complexity | Notes |
|---|---|---|
| BFS with Queue | O(3βΏ Γ 4α΅) | Builds layer-by-layer combinations |
| Iterative using Vector | O(3βΏ Γ 4α΅) | Accumulates results by multiplying with letter sets |
π Related Problemsβ
- Leetcode 17. Letter Combinations of a Phone Number
- Leetcode 22. Generate Parentheses
- Leetcode 39. Combination Sum
- Leetcode 784. Letter Case Permutation
Let me know if you want BFS or Iterative version also!
π¬