-
Notifications
You must be signed in to change notification settings - Fork 0
/
p30.py
58 lines (49 loc) · 1.83 KB
/
p30.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from typing import List, Dict
import unittest
# https://leetcode.cn/problems/substring-with-concatenation-of-all-words/description/
# 哈希表+滑移窗口
# 思路很简单,但是简单的思路运行时间长,有很大的优化空间
class Solution:
@staticmethod
def findSubstring(s: str, words: List[str]) -> List[int]:
# 构建words的哈希表
map1: Dict[str, int] = dict()
for word in words:
map1[word] = map1.get(word, 0) + 1
ans: List[int] = []
len_subs: int = len(words[0]) # 需要哈希的子串的大小
len_w: int = len(words) * len_subs # 窗口大小
# 维护s上的窗口,i是窗口的左端点
for i in range(0, len(s) - len_w + 1):
# 维护窗口内子串的哈希表,j是子串的左端点
map2: Dict[str, int] = dict()
for j in range(i, i + len_w, len_subs):
sub_s: str = s[j : j + len_subs]
map2[sub_s] = map2.get(sub_s, 0) + 1
# 判断哈希表是否相等
if map1 == map2:
ans.append(i)
return ans
class Test(unittest.TestCase):
def test(self) -> None:
self.assertEqual(
Solution.findSubstring("barfoothefoobarman", ["foo", "bar"]), [0, 9]
)
self.assertEqual(
Solution.findSubstring(
"wordgoodgoodgoodbestword", ["word", "good", "best", "word"]
),
[],
)
self.assertEqual(
Solution.findSubstring("barfoofoobarthefoobarman", ["bar", "foo", "the"]),
[6, 9, 12],
)
self.assertEqual(
Solution.findSubstring(
"wordgoodgoodgoodbestword", ["word", "good", "best", "good"]
),
[8],
)
if __name__ == "__main__":
unittest.main()