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

# Longest Word in Dictionary Python Solution

> Tested Python solution for LeetCode 720 with 24 pytest cases. Generate a practice environment with lcpy.

LeetCode 720, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Trie](/catalog/topics/trie), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/longest-word-in-dictionary/description/).

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

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

## Problem

Given an array of strings `words` representing an English Dictionary, return *the longest word in* `words` *that can be built one character at a time by other words in* `words`.

If there is more than one possible answer, return the longest word with the smallest lexicographical order. If there is no answer, return the empty string.

Note that the word should be built from left to right with each additional character being added to the end of a previous word.

### Examples

```
Input: words = ["w","wo","wor","worl","world"]
Output: "world"
```

**Explanation:** The word `"world"` can be built one character at a time by `"w"`, `"wo"`, `"wor"`, and `"worl"`.

```
Input: words = ["a","banana","app","appl","ap","apply","apple"]
Output: "apple"
```

**Explanation:** Both `"apply"` and `"apple"` can be built from other words in the dictionary. However, `"apple"` is lexicographically smaller than `"apply"`.

### Constraints

* `1 <= words.length <= 1000`
* `1 <= words[i].length <= 30`
* `words[i]` consists of lowercase English letters.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(sum of word lengths) (build set) + O(n * L) scan
    # Space: O(sum of word lengths)
    def longest_word(self, words: list[str]) -> str:
        seen: set[str] = set()
        best = ""
        for word in sorted(words):
            if len(word) == 1 or word[:-1] in seen:
                seen.add(word)
                if len(word) > len(best):
                    best = word
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(sum of word lengths) (build set) + O(n \* L) scan | O(sum of word lengths) |

## Tags


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