> For the complete documentation index, see [llms.txt](https://protegejj.gitbook.io/algorithm-practice/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://protegejj.gitbook.io/algorithm-practice/leetcode/backtracking/17-letter-combinations-of-a-phone-number.md).

# 17 Letter Combinations of a Phone Number

## 17. [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/)

## 1. Question

Given a digit string, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below.

![](http://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Telephone-keypad2.svg/200px-Telephone-keypad2.svg.png)

```
Input:
Digit string "23"

Output:
 ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
```

**Note:**\
Although the above answer is in lexicographical order, your answer could be in any order you want.

## 2. Implementation

**(1) Backtracking**

```java
class Solution {
    public List<String> letterCombinations(String digits) {
        List<String> res = new ArrayList<>();

        if (digits == null || digits.length() == 0) {
            return res;
        }

        String[] keys = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

        StringBuilder combination = new StringBuilder();
        getLetterCombinations(digits, 0, combination, keys, res);
        return res;
    }

    public void getLetterCombinations(String digits, int pos, StringBuilder combination, String[] keys, List<String> res) {
        if (pos == digits.length()) {
            res.add(combination.toString());
            return;
        }

        String key  = keys[digits.charAt(pos) - '0'];
        for (int i = 0; i < key.length(); i++) {
            combination.append(key.charAt(i));
            getLetterCombinations(digits, pos + 1, combination, keys, res);
            combination.setLength(combination.length() - 1);
        }
    }
}
```

## 3. Time & Space Complexity

时间复杂度O(L^n), L为digits的长度, n为一个数字对应的最长letters的长度, 空间复杂度O(L^n)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://protegejj.gitbook.io/algorithm-practice/leetcode/backtracking/17-letter-combinations-of-a-phone-number.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
