Prefix and Suffix Search - Problem
You need to design a smart dictionary that can quickly find words based on both their prefix (beginning) and suffix (ending). This is like having a super-powered search engine that can handle queries like "find me all words that start with 'app' and end with 'le'" (which would match 'apple').

Your task is to implement the WordFilter class with two methods:
โ€ข WordFilter(words) - Initialize with an array of dictionary words
โ€ข f(prefix, suffix) - Find the word that matches both conditions and return its largest index (most recent occurrence)

If multiple words match the criteria, return the one with the highest index. If no word matches, return -1.

Example: Given words ["apple", "application", "app"], searching for prefix="app" and suffix="le" should return index 0 ("apple").

Input & Output

example_1.py โ€” Basic Usage
$ Input: WordFilter(["apple"]) f("a", "e")
โ€บ Output: 0
๐Ÿ’ก Note: The word "apple" starts with "a" and ends with "e", so we return its index 0.
example_2.py โ€” Multiple Matches
$ Input: WordFilter(["apple", "app", "application"]) f("app", "")
โ€บ Output: 2
๐Ÿ’ก Note: All three words start with "app" and end with empty string, so we return the largest index 2 ("application").
example_3.py โ€” No Match
$ Input: WordFilter(["apple", "banana"]) f("app", "na")
โ€บ Output: -1
๐Ÿ’ก Note: No word starts with "app" AND ends with "na", so we return -1.

Constraints

  • 1 โ‰ค words.length โ‰ค 104
  • 1 โ‰ค words[i].length โ‰ค 7
  • 1 โ‰ค pref.length, suff.length โ‰ค 7
  • words[i], pref and suff consist of lowercase English letters only
  • At most 104 calls will be made to f

Visualization

Tap to expand
Step 1: Preprocess"apple" (idx 0) โ†’apple#, pple#a, ple#aple#app, e#appl, #apple+ empty prefix/suffix casesStep 2: Build TrieRleStep 3: Queryf("app", "le") โ†’Search: "le#app"Traverse trie pathStep 4: ResultReturn stored index: 0๐ŸŽฏ Key InsightBy storing ALL suffix#prefix combinations, we transform a complex O(Nร—M) searchinto a simple O(P+S) trie traversal - trading space for lightning-fast queries!
Understanding the Visualization
1
Preprocess Words
For each word like 'apple', create all combinations: 'apple#', 'pple#a', 'ple#ap', 'le#app', 'e#appl', '#apple'
2
Build Trie
Insert all combinations into a trie structure, storing the maximum index at each node
3
Handle Queries
For query f('app', 'le'), search for 'le#app' in the trie
4
Return Result
Return the stored index from trie traversal, or -1 if path doesn't exist
Key Takeaway
๐ŸŽฏ Key Insight: Pre-computing all suffix#prefix combinations allows us to answer any prefix-suffix query in O(P+S) time, making this approach optimal for systems with many queries!
Asked in
Google 45 Amazon 32 Microsoft 28 Meta 22
52.3K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen