> For the complete documentation index, see [llms.txt](https://luj.gitbook.io/code/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://luj.gitbook.io/code/dfs/enumeration/generalized-abbreviation.md).

# Generalized Abbreviation

Write a function to generate the generalized abbreviations of a word.

**Note:** The order of the output does not matter.

## **Example**

```
Input: "word"
Output: ["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]
```

## Note

这里考虑一下缩写还是不缩写，开始传入一个空字符串，退出条件是position index（一直累加）达到字符串的长度

* 缩写：记录count加1
* 不缩写：当前字符串加上当前count数再加上当前字符，然后清空count

## Code

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

        helper(word, res, "", 0, 0);

        return res;
    }

    private void helper(String word, List<String> res, String curr,
                        int pos, int count) {
        if (pos == word.length()) {
            res.add(count > 0 ? curr + count : curr);
            return;
        }

        helper(word, res, curr, pos + 1, count + 1);
        helper(word, res, curr + (count > 0 ? count : "") + word.charAt(pos), pos + 1, 0);
    }
}
```


---

# 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://luj.gitbook.io/code/dfs/enumeration/generalized-abbreviation.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.
