> ## 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 through Deleting

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

LeetCode 524, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers), [String](/catalog/topics/string), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/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 524   # by problem number
lcpy gen -s longest_word_in_dictionary_through_deleting   # by problem name
```

## Problem

Given a string `s` and a string array `dictionary`, return *the longest string in the dictionary that can be formed by deleting some of the given string characters*. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.

### Examples

```
Input: s = "abpcplea", dictionary = ["ale","apple","monkey","plea"]
Output: "apple"
```

```
Input: s = "abpcplea", dictionary = ["a","b","c"]
Output: "a"
```

### Constraints

* 1 \<= s.length \<= 1000
* 1 \<= dictionary.length \<= 1000
* 1 \<= dictionary\[i].length \<= 1000
* `s` and `dictionary[i]` consist 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_through_deleting/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/longest_word_in_dictionary_through_deleting/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * (len(s) + len(word)))
    # Space: O(1) extra beyond the answer
    def find_longest_word(self, s: str, dictionary: list[str]) -> str:
        def is_subsequence(word: str) -> bool:
            it = iter(s)
            return all(char in it for char in word)

        best = ""
        for word in dictionary:
            if not is_subsequence(word):
                continue
            if len(word) > len(best) or (len(word) == len(best) and word < best):
                best = word
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(n \* (len(s) + len(word))) | O(1) extra beyond the answer |

## Tags


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