forked from tcandzq/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindAllAnagramsInAString.py
More file actions
73 lines (59 loc) · 2.05 KB
/
FindAllAnagramsInAString.py
File metadata and controls
73 lines (59 loc) · 2.05 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
题号 438 找到字符串中所有字母异位词
给定一个字符串 s 和一个非空字符串 p,找到 s 中所有是 p 的字母异位词的子串,返回这些子串的起始索引。
字符串只包含小写英文字母,并且字符串 s 和 p 的长度都不超过 20100。
说明:
字母异位词指字母相同,但排列不同的字符串。
不考虑答案输出的顺序。
示例 1:
输入:
s: "cbaebabacd" p: "abc"
输出:
[0, 6]
解释:
起始索引等于 0 的子串是 "cba", 它是 "abc" 的字母异位词。
起始索引等于 6 的子串是 "bac", 它是 "abc" 的字母异位词。
示例 2:
输入:
s: "abab" p: "ab"
输出:
[0, 1, 2]
解释:
起始索引等于 0 的子串是 "ab", 它是 "ab" 的字母异位词。
起始索引等于 1 的子串是 "ba", 它是 "ab" 的字母异位词。
起始索引等于 2 的子串是 "ab", 它是 "ab" 的字母异位词。
经典的滑动窗口解法
参考:https://leetcode-cn.com/problems/find-all-anagrams-in-a-string/solution/hua-dong-chuang-kou-tong-yong-si-xiang-jie-jue-zi-/
这个参考的代码可作为一个模板来使用。
"""
from typing import List
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
res = []
left,right = 0,0
needs, window = {}, {}
for char in p:
needs[char] = needs.get(char,0) + 1
match = 0
while right < len(s):
c1 = s[right]
if needs.get(c1):
window[c1] = window.get(c1, 0) + 1
if window[c1] == needs[c1]:
match += 1
right += 1
while match == len(needs):
if right - left == len(p):
res.append(left)
c2 = s[left]
if needs.get(c2):
window[c2] -= 1
if window[c2] < needs[c2]:
match -= 1
left += 1
return res
if __name__ == '__main__':
s = "baa"
p = "aa"
solution = Solution()
print(solution.findAnagrams(s,p))