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
Java
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:
1≤words.length≤104
1≤words[i].length≤7
1≤prefix.length,suffix.length≤7
words[i], prefix, and suffix consist of lowercase English letters only.
At most 104 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