Prefix and Suffix Search
Design a special dictionary that searches words by a prefix and a suffix. Implement WordFilter with constructor that takes words array, and f(prefix, suffix) that returns the index of the word with given prefix and suffix. If multiple words match, return the largest index.

30:00

Prefix and Suffix Search
hard
Topics
Companies

Design a special dictionary that searches words by a prefix and a suffix. Implement WordFilter with constructor that takes words array, and f(prefix, suffix) that returns the index of the word with given prefix and suffix. If multiple words match, return the largest index.

Example 1:
Input: {"words":["apple"],"queries":[["a","e"]]}
Output: [0]
Constraints:
  • 1words.length1041 \leq \text{words.length} \leq 10^4

  • 1words[i].length71 \leq \text{words}[i].\text{length} \leq 7

  • 1prefix.length,suffix.length71 \leq \text{prefix.length}, \text{suffix.length} \leq 7

  • words[i], prefix, and suffix consist of lowercase English letters only.

  • At most 10410^4 calls will be made to f.

Input
arr ={"words":["apple"],"queries":[["a","e"]]}

Words: [apple]

Words Dictionary

[0]

"apple"

Using prefix#suffix as key in hash map for O(1) lookup

Variables
No variables to display
DepthFunction Call
Stack empty
0/1