forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0438.py
31 lines (27 loc) · 753 Bytes
/
0438.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
class Solution:
def findAnagrams(self, s, p):
"""
:type s: str
:type p: str
:rtype: List[int]
"""
from collections import Counter
s_len, p_len = len(s), len(p)
count = p_len
pChar = Counter(p)
result = []
for i in range(s_len):
if pChar[s[i]] >= 1:
count -= 1
pChar[s[i]] -= 1
if i >= p_len:
if pChar[s[i - p_len]] >= 0:
count += 1
pChar[s[i - p_len]] += 1
if count == 0:
result.append(i - p_len + 1)
return result
if __name__ == "__main__":
s = "cbaebabacd"
p = "abc"
print(Solution().findAnagrams(s, p))