How can I print out all possible letter combinations a given phone number can represent?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This problem is a classic backtracking problem: each digit maps to a small set of letters, and the full result is every possible way to choose one letter per digit. The algorithm is simple once you stop thinking in terms of hard-coded cases and instead treat the input as a sequence of branching choices.
The core challenge is not the mapping itself. It is building combinations incrementally while keeping the recursion state clean and handling edge cases such as empty input or unsupported digits.
Model the Keypad First
The standard phone keypad mapping is:
- '
2toabc' - '
3todef' - '
4toghi' - '
5tojkl' - '
6tomno' - '
7topqrs' - '
8totuv' - '
9towxyz'
Digits 0 and 1 usually have no letter mapping in interview versions of the problem. Decide up front whether your function should reject them or ignore them.
Backtracking Solution
A clean way to solve the problem is depth-first search. At each position, choose one letter for the current digit, append it to the partial path, recurse to the next digit, and then remove the letter before trying the next branch.
For input 23, the result is:
Why Backtracking Fits So Well
The search tree mirrors the problem exactly. If the input has n digits, the recursion depth is n. Each level chooses one letter from the current digit's mapping.
That gives you a few nice properties:
- every completed path has the correct length
- no invalid letters are generated
- the algorithm does not need post-filtering
- memory stays proportional to the current path plus the output list
The time complexity is exponential, but that is unavoidable because the number of valid outputs is exponential too.
Iterative Alternative
If you prefer iteration, you can build combinations left to right by expanding a working list.
This is still the same combinatorial expansion, just written without recursion.
Printing Versus Returning
If the only requirement is printing, you can print each combination inside the base case instead of storing it in a list. That avoids holding the full output in memory.
That version is useful if the output is large and you only need side effects such as printing or streaming.
Common Pitfalls
The first mistake is forgetting to handle empty input, which should usually return an empty list. Another is ignoring unsupported digits such as 0 and 1 and then getting a lookup error mid-recursion. Developers also sometimes build strings with repeated concatenation at every step; that works, but a mutable path list is cleaner and avoids extra temporary objects. Finally, some people underestimate the output size. A seven-digit input can already generate thousands of combinations.
Summary
- Build a digit-to-letters map first.
- Use backtracking to choose one letter per digit.
- The algorithm is exponential because the output itself is exponential.
- Validate how your function should handle
0,1, and empty input. - If you only need printing, emit combinations in the base case instead of storing them all.

