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

# Prefix and Suffix Search Python Solution

> Tested Python solution for LeetCode 745 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 745, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Design](/catalog/topics/design), [Trie](/catalog/topics/trie). [View on LeetCode](https://leetcode.com/problems/prefix-and-suffix-search/description/).

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

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

## Problem

Design a special dictionary that searches the words in it by a prefix and a suffix.

Implement the `WordFilter` class:

* `WordFilter(string[] words)` Initializes the object with the `words` in the dictionary.
* `f(string pref, string suff)` Returns *the index of the word in the dictionary,* which has the prefix `pref` and the suffix `suff`. If there is more than one valid index, return **the largest** of them. If there is no such word in the dictionary, return `-1`.

### Examples

```
Input
["WordFilter", "f"]
[[["apple"]], ["a", "e"]]
Output
[null, 0]

Explanation
WordFilter wordFilter = new WordFilter(["apple"]);
wordFilter.f("a", "e"); // return 0, because the word at index 0 has prefix = "a" and suffix = "e".
```

### Constraints

* `1 <= words.length <= 10^4`
* `1 <= words[i].length <= 7`
* `1 <= pref.length, suff.length <= 7`
* `words[i]`, `pref` and `suff` consist of lowercase English letters only.
* At most `10^4` calls will be made to the function `f`.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class WordFilter:
    # Time: __init__ O(n * L^2), f O(P + S)
    # Space: O(n * L^2) keys, each at most 2L + 1 characters
    def __init__(self, words: list[str]) -> None:
        # Encode every (prefix, suffix) pair of every word as "pref#suff". Because
        # words are visited in increasing index order, the last write for a key is the
        # largest matching index, which is exactly what f must return.
        self.best: dict[str, int] = {}
        for index, word in enumerate(words):
            for i in range(len(word) + 1):
                prefix = word[:i]
                for j in range(len(word) + 1):
                    self.best[f"{prefix}#{word[j:]}"] = index

    # Time: O(P + S)
    # Space: O(P + S)
    def f(self, pref: str, suff: str) -> int:
        return self.best.get(f"{pref}#{suff}", -1)
```

## Complexity

| Time | Space |
| - | - |
| **init** O(n \* L^2), f O(P + S) | O(n \* L^2) keys, each at most 2L + 1 characters |

## Tags


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