Letter Combinations of a Phone Number
LeetCode 17 | Difficulty: Mediumβ
MediumProblem Descriptionβ
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 = "2"
Output: ["a","b","c"]
Constraints:
- `1 <= digits.length <= 4`
- `digits[i]` is a digit in the range `['2', '9']`.
Topics: Hash Table, String, Backtracking
Approachβ
Backtrackingβ
Explore all candidates by building solutions incrementally. At each step, choose an option, explore further, then unchoose (backtrack) to try the next option. Prune branches that can't lead to valid solutions.
Generate all combinations/permutations, or find solutions that satisfy constraints.
Hash Mapβ
Use a hash map for O(1) average lookups. Store seen values, frequencies, or indices. The key question: what should I store as key, and what as value?
Need fast lookups, counting frequencies, finding complements/pairs.
String Processingβ
Consider character frequency counts, two-pointer approaches, or building strings efficiently. For pattern matching, think about KMP or rolling hash. For palindromes, expand from center or use DP.
Anagram detection, palindrome checking, string transformation, pattern matching.
Solutionsβ
Solution 1: C# (Best: 248 ms)β
| Metric | Value |
|---|---|
| Runtime | 248 ms |
| Memory | 31.7 MB |
| Date | 2020-01-14 |
public class Solution {
public IList<string> LetterCombinations(string digits) {
string[] map = { "0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
List<string> result = new List<string>();
if(string.IsNullOrEmpty(digits)) return result;
result.Add("");
for (int i = 0; i < digits.Length; i++)
{
List<string> temp = new List<string>();
string val = map[digits[i]-'0'];
for (int j = 0; j < val.Length; j++)
{
for (int k = 0; k < result.Count; k++)
{
temp.Add(result[k] + val[j]);
}
}
result = temp;
}
return result;
}
}
π 1 more C# submission(s)
Submission (2017-07-24) β 572 ms, N/Aβ
public class Solution {
public IList<string> LetterCombinations(string digits) {
string[] map = { "0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
List<string> result = new List<string>();
if(string.IsNullOrEmpty(digits)) return result;
result.Add("");
for (int i = 0; i < digits.Length; i++)
{
List<string> temp = new List<string>();
string val = map[digits[i]-'0'];
for (int j = 0; j < val.Length; j++)
{
for (int k = 0; k < result.Count; k++)
{
temp.Add(result[k] + val[j]);
}
}
result = temp;
}
return result;
}
}
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Backtracking | $O(n! or 2^n)$ | $O(n)$ |
| Hash Map | $O(n)$ | $O(n)$ |
Interview Tipsβ
- Discuss the brute force approach first, then optimize. Explain your thought process.
- Hash map gives O(1) lookup β think about what to use as key vs value.
- Identify pruning conditions early to avoid exploring invalid branches.