Skip to content

Commit b4c2cea

Browse files
committed
+ problem 745
1 parent a6448b3 commit b4c2cea

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# 745. Prefix and Suffix Search
2+
Design a special dictionary that searches the words in it by a prefix and a suffix.
3+
4+
Implement the `WordFilter` class:
5+
* `WordFilter(string[] words)` Initializes the object with the `words` in the dictionary.
6+
* `f(string pref, string suff)` Returns *the index of the word in the dictionary*, which has the prefix `pref` and the suffix `suff`. If there is more than one valid index, return **the largest** of them. If there is no such word in the dictionary, return `-1`.
7+
8+
#### Example 1:
9+
<pre>
10+
<strong>Input:</strong>
11+
["WordFilter", "f"]
12+
[[["apple"]], ["a", "e"]]
13+
<strong>Output:</strong>
14+
[null, 0]
15+
<strong>Explanation:</strong>
16+
WordFilter wordFilter = new WordFilter(["apple"]);
17+
wordFilter.f("a", "e"); // return 0, because the word at index 0 has prefix = "a" and suffix = "e".
18+
</pre>
19+
20+
#### Constraints:
21+
* <code>1 <= words.length <= 104</sup></code>
22+
* `1 <= words[i].length <= 7`
23+
* `1 <= pref.length, suff.length <= 7`
24+
* `words[i]`, `pref` and `suff` consist of lowercase English letters only.
25+
* At most <code>10<sup>4</sup></code> calls will be made to the function `f`.
26+
27+
## Solutions (Python)
28+
29+
### 1. Solution
30+
```Python
31+
class WordFilter:
32+
33+
def __init__(self, words: List[str]):
34+
self.hashmap = {}
35+
36+
for i in range(len(words)):
37+
prefs = [words[i][:j + 1] for j in range(len(words[i]))]
38+
suffs = [words[i][-j - 1:] for j in range(len(words[i]))]
39+
40+
for pref in prefs:
41+
for suff in suffs:
42+
self.hashmap[(pref, suff)] = i
43+
44+
def f(self, pref: str, suff: str) -> int:
45+
return self.hashmap.get((pref, suff), -1)
46+
47+
48+
# Your WordFilter object will be instantiated and called as such:
49+
# obj = WordFilter(words)
50+
# param_1 = obj.f(pref,suff)
51+
```
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# 745. 前缀和后缀搜索
2+
设计一个包含一些单词的特殊词典,并能够通过前缀和后缀来检索单词。
3+
4+
实现 `WordFilter` 类:
5+
* `WordFilter(string[] words)` 使用词典中的单词 `words` 初始化对象。
6+
* `f(string pref, string suff)` 返回词典中具有前缀 `pref` 和后缀 `suff` 的单词的下标。如果存在不止一个满足要求的下标,返回其中 **最大的下标** 。如果不存在这样的单词,返回 `-1`
7+
8+
#### 示例 1:
9+
<pre>
10+
<strong>输入:</strong>
11+
["WordFilter", "f"]
12+
[[["apple"]], ["a", "e"]]
13+
<strong>输出:</strong>
14+
[null, 0]
15+
<strong>解释:</strong>
16+
WordFilter wordFilter = new WordFilter(["apple"]);
17+
wordFilter.f("a", "e"); // 返回 0 ,因为下标为 0 的单词:前缀 prefix = "a" 且 后缀 suffix = "e" 。
18+
</pre>
19+
20+
#### 提示:
21+
* <code>1 <= words.length <= 104</sup></code>
22+
* `1 <= words[i].length <= 7`
23+
* `1 <= pref.length, suff.length <= 7`
24+
* `words[i]``pref``suff` 仅由小写英文字母组成
25+
* 最多对函数 `f` 执行 <code>10<sup>4</sup></code> 次调用
26+
27+
## 题解 (Python)
28+
29+
### 1. 题解
30+
```Python
31+
class WordFilter:
32+
33+
def __init__(self, words: List[str]):
34+
self.hashmap = {}
35+
36+
for i in range(len(words)):
37+
prefs = [words[i][:j + 1] for j in range(len(words[i]))]
38+
suffs = [words[i][-j - 1:] for j in range(len(words[i]))]
39+
40+
for pref in prefs:
41+
for suff in suffs:
42+
self.hashmap[(pref, suff)] = i
43+
44+
def f(self, pref: str, suff: str) -> int:
45+
return self.hashmap.get((pref, suff), -1)
46+
47+
48+
# Your WordFilter object will be instantiated and called as such:
49+
# obj = WordFilter(words)
50+
# param_1 = obj.f(pref,suff)
51+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class WordFilter:
2+
3+
def __init__(self, words: List[str]):
4+
self.hashmap = {}
5+
6+
for i in range(len(words)):
7+
prefs = [words[i][:j + 1] for j in range(len(words[i]))]
8+
suffs = [words[i][-j - 1:] for j in range(len(words[i]))]
9+
10+
for pref in prefs:
11+
for suff in suffs:
12+
self.hashmap[(pref, suff)] = i
13+
14+
def f(self, pref: str, suff: str) -> int:
15+
return self.hashmap.get((pref, suff), -1)
16+
17+
18+
# Your WordFilter object will be instantiated and called as such:
19+
# obj = WordFilter(words)
20+
# param_1 = obj.f(pref,suff)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@
498498
[740][740l] |[Delete and Earn][740] |![rs]
499499
[743][743l] |[Network Delay Time][743] |![rs]
500500
[744][744l] |[Find Smallest Letter Greater Than Target][744] |![rs]
501+
[745][745l] |[Prefix and Suffix Search][745] |![py]
501502
[746][746l] |[Min Cost Climbing Stairs][746] |![rs]
502503
[747][747l] |[Largest Number At Least Twice of Others][747] |![rs]
503504
[748][748l] |[Shortest Completing Word][748] |![py]
@@ -2123,6 +2124,7 @@
21232124
[740]:Problemset/0740-Delete%20and%20Earn/README.md#740-delete-and-earn
21242125
[743]:Problemset/0743-Network%20Delay%20Time/README.md#743-network-delay-time
21252126
[744]:Problemset/0744-Find%20Smallest%20Letter%20Greater%20Than%20Target/README.md#744-find-smallest-letter-greater-than-target
2127+
[745]:Problemset/0745-Prefix%20and%20Suffix%20Search/README.md#745-prefix-and-suffix-search
21262128
[746]:Problemset/0746-Min%20Cost%20Climbing%20Stairs/README.md#746-min-cost-climbing-stairs
21272129
[747]:Problemset/0747-Largest%20Number%20At%20Least%20Twice%20of%20Others/README.md#747-largest-number-at-least-twice-of-others
21282130
[748]:Problemset/0748-Shortest%20Completing%20Word/README.md#748-shortest-completing-word
@@ -3742,6 +3744,7 @@
37423744
[740l]:https://leetcode.com/problems/delete-and-earn/
37433745
[743l]:https://leetcode.com/problems/network-delay-time/
37443746
[744l]:https://leetcode.com/problems/find-smallest-letter-greater-than-target/
3747+
[745l]:https://leetcode.com/problems/prefix-and-suffix-search/
37453748
[746l]:https://leetcode.com/problems/min-cost-climbing-stairs/
37463749
[747l]:https://leetcode.com/problems/largest-number-at-least-twice-of-others/
37473750
[748l]:https://leetcode.com/problems/shortest-completing-word/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@
498498
[740][740l] |[删除并获得点数][740] |![rs]
499499
[743][743l] |[网络延迟时间][743] |![rs]
500500
[744][744l] |[寻找比目标字母大的最小字母][744] |![rs]
501+
[745][745l] |[前缀和后缀搜索][745] |![py]
501502
[746][746l] |[使用最小花费爬楼梯][746] |![rs]
502503
[747][747l] |[至少是其他数字两倍的最大数][747] |![rs]
503504
[748][748l] |[最短完整词][748] |![py]
@@ -2123,6 +2124,7 @@
21232124
[740]:Problemset/0740-Delete%20and%20Earn/README_CN.md#740-删除并获得点数
21242125
[743]:Problemset/0743-Network%20Delay%20Time/README_CN.md#743-网络延迟时间
21252126
[744]:Problemset/0744-Find%20Smallest%20Letter%20Greater%20Than%20Target/README_CN.md#744-寻找比目标字母大的最小字母
2127+
[745]:Problemset/0745-Prefix%20and%20Suffix%20Search/README_CN.md#745-前缀和后缀搜索
21262128
[746]:Problemset/0746-Min%20Cost%20Climbing%20Stairs/README_CN.md#746-使用最小花费爬楼梯
21272129
[747]:Problemset/0747-Largest%20Number%20At%20Least%20Twice%20of%20Others/README_CN.md#747-至少是其他数字两倍的最大数
21282130
[748]:Problemset/0748-Shortest%20Completing%20Word/README_CN.md#748-最短完整词
@@ -3742,6 +3744,7 @@
37423744
[740l]:https://leetcode.cn/problems/delete-and-earn/
37433745
[743l]:https://leetcode.cn/problems/network-delay-time/
37443746
[744l]:https://leetcode.cn/problems/find-smallest-letter-greater-than-target/
3747+
[745l]:https://leetcode.cn/problems/prefix-and-suffix-search/
37453748
[746l]:https://leetcode.cn/problems/min-cost-climbing-stairs/
37463749
[747l]:https://leetcode.cn/problems/largest-number-at-least-twice-of-others/
37473750
[748l]:https://leetcode.cn/problems/shortest-completing-word/

0 commit comments

Comments
 (0)