Prefix and Suffix Search
Design a special dictionary that searches words by a prefix and a suffix.
Implement the WordFilter class:
WordFilter(words)initializes the object with thewordsin the dictionary.f(prefix, suffix)returns the index of the word in the dictionary that has prefixprefixand suffixsuffix. If there is more than one valid index, return the largest of them. If there is no such word, return-1.
Examples
Input: []
Output: 0
Input: ["a"]
Output: 0
Hints
Preprocess each word so a combined key can represent both prefix and suffix queries.
A wrapped key like `suffix + '{' + word` allows prefix matching over all split positions.
Insert keys for all suffix splits and store the latest index.
Related Problems
Prefix and Suffix Search
Design a special dictionary that searches words by a prefix and a suffix.