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

# Q17. Letter Combinations of a Phone Number

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.

## 分析

递归求解。

## C++代码

```cpp
class Solution {
public:
    void letterCombinations_helper(vector<string>&res, string digits, int idx, string path){
        vector<string> phone = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        string str = phone[digits[idx]-'2'];
        for(int i=0; i<str.size(); i++){
            if(idx == digits.size() -1){
                res.push_back(path+str[i]);
            }else{
                letterCombinations_helper(res, digits, idx+1, path+str[i]);
            }
        }
    }
    vector<string> letterCombinations(string digits) {
        vector<string> res;
        int idx = 0;
        string path = "";
        letterCombinations_helper(res, digits, idx, path);
        return res;
    }
};
```


---

# 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://senliuy.gitbook.io/leetcode/di-san-zhang/q17-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.
