Skip to main content

Letter Combination Builder

easy
BacktrackingStringRecursionBacktracking Combinations
Asked atAmazonGoogleMicrosoftMeta

Problem Description

On a classic phone keypad, each digit maps to a set of letters:

- `2` → `"abc"`, `3` → `"def"`, `4` → `"ghi"`, `5` → `"jkl"`
- `6` → `"mno"`, `7` → `"pqrs"`, `8` → `"tuv"`, `9` → `"wxyz"`

Given a string `digits` containing digits from `2–9`, return **all possible letter combinations** that the digit sequence could represent. Return the answer in any order.

If `digits` is empty, return an empty list.

**Example 1:**
```
Input: digits = "34"
Output: ["dg","dh","di","eg","eh","ei","fg","fh","fi"]
```

**Example 2:**
```
Input: digits = "7"
Output: ["p","q","r","s"]
```

Constraints

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

Follow-up

Can you solve this iteratively without recursion, building combinations level by level?

Hints

Try the problem first. If you get stuck, you can reveal hints one at a time.

Solution

Leaderboard

No entries yet for python.

Be the first — submit an accepted solution.