TRT
Sign in
mediumCoreLeetCode#backtracking#cartesian product

Letter combinations of a phone number

Return every string a sequence of phone digits could spell, one letter per digit.

The Runtime Theory Team1 min read
Taught inBacktracking basicsPro — members onlyBacktracking builds a solution one choice at a time, explores each choice recursively and undoes it afterwards. Pruning dead ends early is what makes it practical.
Solve it

Solving happens on the judge — come back and mark it done

Sample cases

indigits = "23"

out["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]

indigits = ""

out[]

indigits = "7"

out["p", "q", "r", "s"]

Given a string of digits from 2 to 9, return all letter combinations they could represent on a phone keypad (2 is abc, 3 is def, … 7 is pqrs, 9 is wxyz).

Before you code

Each digit is one level of the search tree, and its letters are the choices at that level. The result has up to 4ⁿ strings for n digits.

Hints

  1. Recurse with the index of the next digit and the letters chosen so far.
  2. When the index reaches the end, record the string.
  3. An empty input returns an empty list, not a list with one empty string.

Production & complexity notes

When solving Letter combinations of a phone number on LeetCode (medium), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in Backtracking basics to reinforce the underlying pattern.

What it practices

The choose, explore, unchoose pattern and pruning, from Backtracking basics.

Not started

Sign in to save your learning progress.

Sign in to save