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

# Short Encoding of Words Python Solution

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

LeetCode 820, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Trie](/catalog/topics/trie). [View on LeetCode](https://leetcode.com/problems/short-encoding-of-words/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 820   # by problem number
lcpy gen -s short_encoding_of_words   # by problem name
```

## Problem

A **valid encoding** of an array of `words` is any reference string `s` and array of indices `indices` such that:

* `words.length == indices.length`
* The reference string `s` ends with the `'#'` character.
* For each index `indices[i]`, the **substring** of `s` starting from `indices[i]` and up to (but not including) the next `'#'` character is equal to `words[i]`.

Given an array of `words`, return *the **length of the shortest reference string** \* `s` \* possible of any **valid encoding** of \* `words`*.

### Examples

```
Input: words = ["time", "me", "bell"]
Output: 10
Explanation: A valid encoding would be s = "time#bell#" and indices = [0, 2, 5].
words[0] = "time", the substring of s starting from indices[0] = 0 to the next '#' is underlined in "time#bell#"
words[1] = "me", the substring of s starting from indices[1] = 2 to the next '#' is underlined in "ti*me*#bell#"
words[2] = "bell", the substring of s starting from indices[2] = 5 to the next '#' is underlined in "time#*bell*#"
```

```
Input: words = ["t"]
Output: 2
Explanation: A valid encoding would be s = "t#" and indices = [0].
```

### Constraints

* 1 \<= words.length \<= 2000
* 1 \<= words\[i].length \<= 7
* words\[i] consists of only lowercase letters.

**Follow up:** Can you solve it in O(n \* max(words\[i].length)) time using a Trie?

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * L^2) where n = len(words), L = max word length (suffix slices)
    # Space: O(n * L)
    def minimum_length_encoding(self, words: list[str]) -> int:
        unique = set(words)
        return sum(
            len(word) + 1
            for word in unique
            if not any(other.endswith(word) for other in unique if other != word)
        )
```

## Complexity

| Time | Space |
| - | - |
| O(n \* L^2) where n = len(words), L = max word length (suffix slices) | O(n \* L) |

## Tags


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