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

# Unique Word Abbreviation Python Solution

> Tested Python solution for LeetCode 288 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 288, [Medium](/catalog/medium). Topics: [Design](/catalog/topics/design), [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/unique-word-abbreviation/description/).

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

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

## Problem

The **abbreviation** of a word is a concatenation of its first letter, the number of characters between the first and last letter, and its last letter. If a word has only two characters, then it is an **abbreviation** of itself.

For example:

* `dog -> d1g` because there is one letter between the first letter `d` and the last letter `g`.
* `internationalization -> i18n` because there are 18 letters between the first letter `i` and the last letter `n`.
* `it -> it` because any word with only two characters is an **abbreviation** of itself.

Implement the `ValidWordAbbr` class:

* `ValidWordAbbr(String[] dictionary)` Initializes the object with a `dictionary` of words.
* `boolean isUnique(string word)` Returns `true` if **either** of the following conditions are met (otherwise returns `false`):
  * There is no word in `dictionary` whose **abbreviation** is equal to `word`'s **abbreviation**.
  * For any word in `dictionary` whose **abbreviation** is equal to `word`'s **abbreviation**, that word and `word` are **the same**.

### Examples

```
Input
["ValidWordAbbr", "isUnique", "isUnique", "isUnique", "isUnique", "isUnique"]
[[["deer", "door", "cake", "card"]], ["dear"], ["cart"], ["cane"], ["make"], ["cake"]]
Output
[null, false, true, false, true, true]

Explanation
ValidWordAbbr validWordAbbr = new ValidWordAbbr(["deer", "door", "cake", "card"]);
validWordAbbr.isUnique("dear"); // return false, "deer" and "dear" share "d2r" but differ.
validWordAbbr.isUnique("cart"); // return true, no dictionary word abbreviates to "c2t".
validWordAbbr.isUnique("cane"); // return false, "cake" and "cane" share "c2e" but differ.
validWordAbbr.isUnique("make"); // return true, no dictionary word abbreviates to "m2e".
validWordAbbr.isUnique("cake"); // return true, "cake" is the only word with "c2e".
```

### Constraints

* `1 <= dictionary.length <= 3 * 10^4`
* `1 <= dictionary[i].length <= 20`
* `dictionary[i]` consists of lowercase English letters.
* `1 <= word.length <= 20`
* `word` consists of lowercase English letters.
* At most `5000` calls will be made to `isUnique`.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import defaultdict


class ValidWordAbbr:
    # Time: __init__ O(n), is_unique O(1) where n is the dictionary size
    # Space: O(n) for the abbreviation-to-words map
    def __init__(self, dictionary: list[str]) -> None:
        self.groups: dict[str, set[str]] = defaultdict(set)
        for word in dictionary:
            self.groups[self.abbr(word)].add(word)

    # Time: O(1)
    # Space: O(1)
    def is_unique(self, word: str) -> bool:
        group = self.groups.get(self.abbr(word))
        return group is None or group == {word}

    # Time: O(1)
    # Space: O(1)
    def abbr(self, word: str) -> str:
        return word if len(word) < 3 else word[0] + str(len(word) - 2) + word[-1]
```

## Complexity

| Time | Space |
| - | - |
| **init** O(n), is\_unique O(1) where n is the dictionary size | O(n) for the abbreviation-to-words map |

## Tags


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