# Problem 320: Generalized Abbreviation

> <https://leetcode.com/problems/generalized-abbreviation/#/description>

![](/files/-Lpv9v2xrygaRsOmR0qX)

## 思路

* 对每个 character 来说，有两种情况：取或者不取。
* 对于每个 dfs，也有两种情况：count 增加，或者从 0 开始。

```java
public class Solution {
    public List<String> generateAbbreviations(String word) {
        List<String> rst = new ArrayList<>();
        dfs(word, "", 0, 0, rst);

        return rst;
    }

    private void dfs(String word, String cur, int pos, int count, List<String> rst) {
        if (pos == word.length()) {
            if (count > 0) cur += count;
            rst.add(cur);
            return;
        } 

        dfs(word, cur, pos + 1, count + 1, rst);
        dfs(word, cur + (count > 0 ? count : "") + word.charAt(pos), pos + 1, 0, rst);
    }
}
```


---

# Agent Instructions: 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://liuyang89116.gitbook.io/my-leetcode-book/chapter_1_combination_and_permutation/problem-320-generalized-abbreviation.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.
