You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given an array of strings words representing an English Dictionary, return the longest word in words that can be built one character at a time by other words in words.
5
+
6
+
If there is more than one possible answer, return the longest word with the smallest lexicographical order. If there is no answer, return the empty string.
7
+
8
+
9
+
10
+
Example 1:
11
+
12
+
Input: words = ["w","wo","wor","worl","world"]
13
+
Output: "world"
14
+
Explanation: The word "world" can be built one character at a time by "w", "wo", "wor", and "worl".
15
+
Example 2:
16
+
17
+
Input: words = ["a","banana","app","appl","ap","apply","apple"]
18
+
Output: "apple"
19
+
Explanation: Both "apply" and "apple" can be built from other words in the dictionary. However, "apple" is lexicographically smaller than "apply".
Design a special dictionary with some words that searchs the words in it by a prefix and a suffix.
5
+
6
+
Implement the WordFilter class:
7
+
8
+
WordFilter(string[] words) Initializes the object with the words in the dictionary.
9
+
f(string prefix, string suffix) Returns the index of the word in the dictionary, which has the prefix prefix and the suffix suffix. If there is more than one valid index, return the largest of them. If there is no such word in the dictionary, return -1.
10
+
11
+
12
+
Example 1:
13
+
14
+
Input
15
+
["WordFilter", "f"]
16
+
[[["apple"]], ["a", "e"]]
17
+
Output
18
+
[null, 0]
19
+
20
+
Explanation
21
+
WordFilter wordFilter = new WordFilter(["apple"]);
22
+
wordFilter.f("a", "e"); // return 0, because the word at index 0 has prefix = "a" and suffix = 'e".
23
+
24
+
25
+
Constraints:
26
+
27
+
1 <= words.length <= 15000
28
+
1 <= words[i].length <= 10
29
+
1 <= prefix.length, suffix.length <= 10
30
+
words[i], prefix and suffix consist of lower-case English letters only.
31
+
At most 15000 calls will be made to the function f.
32
+
"""
33
+
34
+
# THIS SOLUTION WILL RESULT IN TIME LIMIT EXCEEDED ERROR.
0 commit comments