Skip to main content

Letter Combinations of a Phone Number

LeetCode 17 | Difficulty: Medium​

Medium

Problem 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.

When to use

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?

When to use

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.

When to use

Anagram detection, palindrome checking, string transformation, pattern matching.


Solutions​

Solution 1: C# (Best: 248 ms)​

MetricValue
Runtime248 ms
Memory31.7 MB
Date2020-01-14
Solution
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​

ApproachTimeSpace
Backtracking$O(n! or 2^n)$$O(n)$
Hash Map$O(n)$$O(n)$

Interview Tips​

Key Points
  • 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.