mediumBacktrackingBacktracking

Letter Combinations of a Phone Number

mediumTime: O(4^n * n)Space: O(n)

Signals to notice

map digits to lettersgenerate all possible stringscombinatorial explosion

Brute force first

Not applicable — generating all combinations IS the problem. Output size is up to 4^n. That direct path helps you understand the question, but it tends to treat every possibility as brand new instead of learning from earlier steps.

The key insight

Backtracking: for each digit, try each possible letter mapping. Build the string character by character. When the string reaches input length, record it. The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.

What must stay true

Each digit maps to 3-4 letters. At each position, try all possible letters for the current digit, then recurse for the next digit. The branching factor is the number of letters per digit. If that remains true after every update, the rest of the reasoning has a stable place to stand.

Easy way to go wrong

Not handling the empty input case — return an empty array, not an array with an empty string. The fix is usually to return to the meaning of each move, not just the steps themselves.

Backtracking Pattern