> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Generalized Abbreviation Python Solution

> Tested Python solution for LeetCode 320 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 320, [Medium](/catalog/medium). Topics: [Bit Manipulation](/catalog/topics/bit-manipulation), [String](/catalog/topics/string), [Backtracking](/catalog/topics/backtracking). [View on LeetCode](https://leetcode.com/problems/generalized-abbreviation/description/).

Generate this problem as a practice environment: tested reference solution, 12 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 320   # by problem number
lcpy gen -s generalized_abbreviation   # by problem name
```

## Problem

A word's **generalized abbreviation** can be constructed by taking any number of **non-overlapping** and **non-adjacent** substrings and replacing them with their respective lengths.

* For example, `"abcde"` can be abbreviated into:
  * `"a3e"` (`"bcd"` turned into `"3"`)
  * `"1bcd1"` (`"a"` and `"e"` both turned into `"1"`)
  * `"5"` (`"abcde"` turned into `"5"`)
  * `"abcde"` (no substrings replaced)
* However, these abbreviations are **invalid**:
  * `"23"` (`"ab"` turned into `"2"` and `"cde"` turned into `"3"`) is invalid as the substrings chosen are adjacent.
  * `"22de"` (`"ab"` turned into `"2"` and `"bc"` turned into `"2"`) is invalid as the substring chosen overlap.

Given a string `word`, return *a list of all the possible **generalized abbreviations** of* `word`. Return the answer in **any order**.

### Examples

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

```
Input: word = "a"
Output: ["1","a"]
```

### Constraints

* `1 <= word.length <= 15`
* `word` consists of only lowercase English letters.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/generalized_abbreviation/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/generalized_abbreviation/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(2^n * n) — one output string per bitmask, built in O(n)
    # Space: O(2^n * n) — output list dominates
    def generate_abbreviations(self, word: str) -> list[str]:
        n = len(word)
        result: list[str] = []
        for mask in range(1 << n):
            parts: list[str] = []
            run = 0
            for i, ch in enumerate(word):
                if mask >> i & 1:
                    run += 1
                else:
                    if run:
                        parts.append(str(run))
                        run = 0
                    parts.append(ch)
            if run:
                parts.append(str(run))
            result.append("".join(parts))
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(2^n \* n) — one output string per bitmask, built in O(n) | O(2^n \* n) — output list dominates |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.