Skip to main content

Letter Combinations of a Phone Number - Solution & Explanation

MediumHash TableStringBacktracking30 min readAsked at: Amazon, Microsoft, Apple +31
Practice this problem

Problem Statement

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 = ""
Output: []

Example 3:

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

 

Constraints:

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

Approach Overview

Problem Overview: Given a string of digits from 2-9, return all possible letter combinations those digits could represent on a classic phone keypad. Each digit maps to multiple characters, and the task is to generate every valid combination in the correct order.

Approach 1: Recursive Backtracking (O(4^n * n) time, O(n) recursion space)

This problem maps directly to backtracking. Each digit expands into multiple possible characters, forming a decision tree where every level corresponds to one digit. Start with the first digit, iterate through its mapped letters, append one character to the current path, and recursively process the next digit. When the path length equals the number of digits, add the built string to the result list. The branching factor is at most 4 (digits like 7 and 9), giving roughly 4^n combinations, and building each string costs O(n). A small hash table or array stores the digit-to-letter mapping. This approach is concise, natural for the problem structure, and commonly expected in interviews.

Approach 2: Iterative Breadth-First Search (BFS) (O(4^n * n) time, O(4^n) space)

The same combination tree can be generated iteratively using a queue. Start by pushing an empty string into the queue. For each digit in the input, process all current partial combinations: remove a string from the queue, append every possible letter mapped from the current digit, and push the new strings back. After processing all digits, the queue contains every complete combination. This effectively performs a level-by-level traversal of the combination tree, similar to BFS. The algorithm still generates up to 4^n results and builds strings of length n, leading to O(4^n * n) time complexity. The queue stores many intermediate strings, so space usage grows to O(4^n).

Both approaches rely on simple string construction and a digit-to-character lookup table. Backtracking builds combinations depth-first using recursion, while BFS expands them iteratively layer by layer.

Recommended for interviews: Recursive backtracking is the expected solution. It mirrors the natural combinatorial structure of the keypad mapping and uses minimal extra memory. BFS works as well and shows strong understanding of iterative state expansion, but interviewers usually look for the backtracking pattern when exploring all combinations of choices.

Approach 1: Recursive Backtracking Approach

This approach uses recursion to explore all possible combinations represented by the digit-to-letter mapping of a phone keypad. We incrementally build letter combinations by allocating one letter per digit, and then explore further by making recursive calls.

The recursion depth is determined by the number of digits and the choices per digit. At each step, the algorithm branches into as many recursive calls as there are letters assigned to the current digit, continuing until all digits have been processed.

This Python solution utilizes a recursive helper method backtrack to explore all combinations by trying each possible letter for each digit sequentially. The base case is when the length of the path equals the number of digits, signifying a complete combination has been constructed. The combinations list accumulates all completed combinations, which is then returned as the result.

Code

Python

Java

C++

C

C#

JavaScript

Complexity

Time Complexity: O(3^n * 4^m), where n is the number of digits that map to 3 letters, and m is the number of digits that map to 4 letters.

Space Complexity: O(n), the maximum depth of the recursion stack is n.

Try this approach in the editor →

Approach 2: Iterative Breadth-First Search (BFS) Approach

The iterative BFS approach systematically explores all possible combinations of letter sequences by progressively building up combinations using a queue. It starts by enqueueing an empty string, and then, for each digit from the input, extends through all mapped letters, enqueuing partial solutions until all digits have been processed.

This method mimics the complete depth-first search but layers on combinations iteratively, capitalizing on the breadth search over immediate digit map expansion.

The Python solution leverages a queue (deque) to perform BFS-style exploration of all letter combinations. Initially, it stores an empty string, and iteratively enqueues combinations by appending the letters mapped from current digits. The process continues until all digits have been covered, emerging with a full spectrum of possible sequences.

Code

Python

Java

C++

C

C#

JavaScript

Complexity

Time Complexity: O(3^n * 4^m), as methodically layers through the digit-letter expansion uniformly across a queue.

Space Complexity: O(n * 3^n * 4^m), where queue storage inflates in tandem with combination depth.

Try this approach in the editor →

Approach 3: Traversal

First, we use an array or hash table to store the letters corresponding to each digit. Then we traverse each digit, combine its corresponding letters with the previous results to get the new results.

The time complexity is O(4^n), and the space complexity is O(4^n). Here, n is the length of the input digits.

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

C#

Try this approach in the editor →

Approach 4: DFS

We can use the method of depth-first search to enumerate all possible letter combinations. Suppose that a part of the letter combination has been generated, but some digits have not been exhausted. At this time, we take out the letters corresponding to the next digit, and then enumerate each letter corresponding to this digit one by one, add them to the letter combination that has been generated before, to form all possible combinations.

The time complexity is O(4^n), and the space complexity is O(n). Here, n is the length of the input digits.

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

C#

PHP

C

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Recursive Backtracking Approach

Time Complexity: O(3^n * 4^m), where n is the number of digits that map to 3 letters, and m is the number of digits that map to 4 letters.

Space Complexity: O(n), the maximum depth of the recursion stack is n.

Iterative Breadth-First Search (BFS) Approach

Time Complexity: O(3^n * 4^m), as methodically layers through the digit-letter expansion uniformly across a queue.

Space Complexity: O(n * 3^n * 4^m), where queue storage inflates in tandem with combination depth.

Traversal—
DFS—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Recursive BacktrackingO(4^n * n)O(n)Best for interviews and when generating combinations via DFS recursion
Iterative BFS (Queue)O(4^n * n)O(4^n)Useful when you prefer iterative solutions or want level-by-level combination expansion

Video Solution

Letter Combinations of a Phone Number - Backtracking - Leetcode 17 • NeetCode • 237,557 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Letter Combinations of a Phone Number easy or hard?
The problem is generally rated Medium difficulty. The logic is straightforward once you recognize the backtracking pattern, but it tests your ability to generate combinations and manage recursion correctly.
Letter Combinations of a Phone Number Python/Java solution
In Python or Java, the typical solution uses a map from digits to characters and a recursive backtracking function that builds combinations one character at a time. Many implementations also provide an iterative BFS version using a queue.
How to solve Letter Combinations of a Phone Number in O(n)?
The problem cannot be solved in strict O(n) time because all possible combinations must be generated. The optimal approach is backtracking or BFS with O(4^n * n) time, which is proportional to the total number of combinations produced.
What is the best approach for Letter Combinations of a Phone Number?
Recursive backtracking is the most common and preferred solution. Each digit expands into multiple letters, forming a decision tree where every level represents a digit. Backtracking explores all paths while building combinations incrementally. The time complexity is O(4^n * n), where n is the number of digits.
Is Letter Combinations of a Phone Number asked at Google/Amazon/Meta?
Letter Combinations of a Phone Number appears frequently in technical interviews at companies like Amazon, Google, and Meta. It tests understanding of recursion, backtracking, and generating combinations from a decision tree.
What data structure is used in Letter Combinations of a Phone Number?
A hash map or array is typically used to store the digit-to-letter keypad mapping. The algorithm then uses recursion (call stack) or a queue for BFS to generate combinations of strings.
What is the time complexity of Letter Combinations of a Phone Number?
The time complexity is O(4^n * n). Each digit can map to up to four letters (like 7 and 9), producing at most 4^n combinations. Constructing each combination requires O(n) time to build the final string.

Ready to solve this problem?

Practice Letter Combinations of a Phone Number with our built-in code editor and test cases.

Practice on FleetCode