Skip to content
AI360Xpert
Beta
Difficulty: MediumBacktracking

Letter Combinations of a Phone Number

Problem in Plain English

Given a digit string from 2 to 9, return every letter combination it can spell with the telephone keypad mapping. An empty digit string yields an empty list.

Problem Statement

Given a string digits holding characters from 2 to 9, return every letter string those digits can spell on a classic telephone keypad.

Each digit maps to its letters, for example 2 gives abc and 3 gives def, and each position in the answer takes one letter from its own digit.

Return an empty list when digits is empty; otherwise the answer order does not matter.

Constraints

  • 0 <= digits.length <= 4
  • digits[i] is a digit in the range 2 to 9.

Examples

Example 1

Input
digits = "23"
Output
["ad","ae","af","bd","be","bf","cd","ce","cf"]

Digit 2 offers a, b, c and digit 3 offers d, e, f. Fix a and pair it with d, e, f, then repeat for b and c, giving 3×3=93 \times 3 = 9 distinct two-letter answers.

Example 2

Input
digits = ""
Output
[]

The problem explicitly requires no answers when no digits are provided. The internal empty prefix used to start either algorithm is a construction tool, not a result, so return [] rather than [""].

Example 3

Input
digits = "2"
Output
["a","b","c"]

With only one position, each mapped letter already completes an answer. Digit 2 supplies exactly a, b, c, and there is no second position to extend.

Example 4

Input
digits = "77"
Output
["pp","pq","pr","ps","qp","qq","qr","qs","rp","rq","rr","rs","sp","sq","sr","ss"]

Both positions independently choose from pqrs, so there are 4×4=164 \times 4 = 16 answers. Repeated digits do not make the positions interchangeable: pq and qp are different strings, while pp is also valid because letters may be reused.

Intuition

Each answer chooses exactly one letter for each digit position. For 23, the first position offers abc and the second offers def, so each of the three first letters pairs with all three second letters. This is a Cartesian product: all combinations of independent position choices. You can build every partial string a layer at a time, or explore one partial string depth-first and undo its last letter before trying a sibling. No branch needs pruning because every choice is valid; the work comes from writing all answers.

Approaches

Level Expansion

Solution Details

Reveal Level Expansion: intuition, complexity, and code

Hints

Hint 1
Each answer position draws from exactly one digit, so the answer count is the product of the per-digit letter counts.
Hint 2
Handle one digit per recursion level and loop over that digit's letters inside the level.
Hint 3
Append a letter, recurse to the next digit, then remove the letter; join the buffer into a string only when every digit is consumed.

Edge Cases

  • Empty input "" has no digits to spell anything, so return an empty list, never a list with one empty string.
  • A single digit like "7" with four letters returns four one-letter strings ["p", "q", "r", "s"].
  • Digit "9" maps to four letters wxyz while most digits map to three, which is why the bound uses four to the n.
  • Repeated digits such as 77 represent independent positions; repeated letters and reversed pairs are valid. The maximum input 7777 returns 256 distinct strings of length four.

Common Mistakes and Interview Tips

  • Forgetting the empty-input guard and returning a list with one empty string.
  • Sharing one mutable buffer across answers without copying at the leaf, so every entry ends up spelling the last combination.
  • Skipping the undo step after recursing, which glues letters from sibling branches into one long string.
  • Extending the current layer while iterating over it, which can append the same digit repeatedly; build a separate next layer and commit after the loops.
  • Assuming every digit has three letters drops the fourth choices on 7 and 9. Sorting or deduplicating letters globally also destroys independent position choices.
  • Interview tip: distinguish O(n) DFS auxiliary space from the O(nP) returned strings, where P is the product of per-digit letter counts. Enumeration cannot avoid writing the output, so DFS improves intermediate storage rather than the output-sized time bound.

Key Takeaway

Per-position choices over a fixed alphabet are a depth-first walk: one buffer, one level per position, and a join at each leaf.