> For the complete documentation index, see [llms.txt](https://mnunknown.gitbook.io/algorithm-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mnunknown.gitbook.io/algorithm-notes/search_and_backtracking_sou_suo_yu_hui_su/fb_phone_letter_combination.md).

# (FB) Phone Letter Combination

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

### 把这题单独拿出来，是因为这是个非常典型的递归 / 迭代都很直观的搜索类问题，一个 DFS，一个 BFS.

### BFS 就靠 Queue，以 queue 首长度 == i 来判断层数，反复做 join. 另外维护一个 String\[] 用作字典查询。

```java
    public List<String> letterCombinations(String digits) {
        LinkedList<String> queue = new LinkedList<String>();
        if(digits == null || digits.length() == 0) return queue;

        queue.add("");
        String[] letters = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

        for(int i = 0; i < digits.length(); i++){
            int cur = digits.charAt(i) - '0';
            while(queue.peek().length() == i){
                String str = queue.remove();
                char[] candidates = letters[cur].toCharArray();
                for(char chr : candidates){
                    queue.add(str + chr);
                }
            }
        }

        return queue;
    }
```

### DFS 写法就是做了无数次的 backtracking 写法。本质上，都是殊途同归的状态空间搜索罢了。

```java
    public List<String> letterCombinations(String digits) {
        List<String> list = new ArrayList<String>();
        if(digits == null || digits.length() == 0) return list;
        String[] letters = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

        dfs(list, new StringBuilder(), digits, letters, 0);

        return list;
    }

    private void dfs(List<String> list, StringBuilder sb, String digits, String[] letters, int pos){
        if(sb.length() == digits.length()){
            list.add(sb.toString());
            return;
        }

        for(int i = pos; i < digits.length(); i++){
            String str = letters[digits.charAt(i) - '0'];
            for(int j = 0; j < str.length(); j++){
                int length = sb.length();
                sb.append(str.charAt(j));
                dfs(list, sb, digits, letters, i + 1);
                sb.setLength(length);
            }
        }
    }
```


---

# 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, and the optional `goal` query parameter:

```
GET https://mnunknown.gitbook.io/algorithm-notes/search_and_backtracking_sou_suo_yu_hui_su/fb_phone_letter_combination.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
