> ## 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.

# Word Abbreviation Python Solution with Tests

> Tested Python solution for LeetCode 527 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 527, [Hard](/catalog/hard). Topics: [Greedy](/catalog/topics/greedy), [Trie](/catalog/topics/trie), [Array](/catalog/topics/array), [String](/catalog/topics/string), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/word-abbreviation/description/).

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

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

## Problem

Given an array of **distinct** strings `words`, return the minimal possible **abbreviations** for every word.

The following are the rules for a string abbreviation:

1. The **initial** abbreviation for each word is: the first character, then the number of characters in between, followed by the last character.
2. If more than one word shares the **same** abbreviation, then perform the following operation:
   * **Increase** the prefix (characters in the first part) of each of their abbreviations by `1`.
     * For example, say you start with the words `["abcdef","abndef"]` both initially abbreviated as `"a4f"`. Then, a sequence of operations would be `["a4f","a4f"]` -> `["ab3f","ab3f"]` -> `["abc2f","abn2f"]`.
   * This operation is repeated until every abbreviation is **unique**.
3. At the end, if an abbreviation did not make a word shorter, then keep it as the original word.

### Examples

```
Input: words = ["like","god","internal","me","internet","interval","intension","face","intrusion"]
Output: ["l2e","god","internal","me","i6t","interval","inte4n","f2e","intr4n"]
```

```
Input: words = ["aa","aaa"]
Output: ["aa","aaa"]
```

### Constraints

* `1 <= words.length <= 400`
* `2 <= words[i].length <= 400`
* `words[i]` consists of lowercase English letters.
* All the strings of `words` are **unique**.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * L^2) worst case over group resolution rounds
    # Space: O(n * L)
    def words_abbreviation(self, words: list[str]) -> list[str]:
        n = len(words)
        prefix = [1] * n
        while True:
            groups: dict[str, list[int]] = {}
            for i, word in enumerate(words):
                if prefix[i] > len(word) - 2:
                    continue
                abbrev = word[: prefix[i]] + str(len(word) - prefix[i] - 1) + word[-1]
                groups.setdefault(abbrev, []).append(i)
            conflicts = [group for group in groups.values() if len(group) > 1]
            if not conflicts:
                break
            for group in conflicts:
                for i in group:
                    prefix[i] += 1
        result = []
        for i, word in enumerate(words):
            if prefix[i] > len(word) - 2:
                result.append(word)
            else:
                abbrev = word[: prefix[i]] + str(len(word) - prefix[i] - 1) + word[-1]
                result.append(abbrev if len(abbrev) < len(word) else word)
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n \* L^2) worst case over group resolution rounds | O(n \* L) |

## Tags


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