-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
32 lines (25 loc) · 821 Bytes
/
solution.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
import collections
from typing import List
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
res = []
p_count = collections.Counter(p)
distincts = len(p_count)
left, right = 0, 0
while right < len(s):
if s[right] in p_count:
p_count[s[right]] -= 1
if p_count[s[right]] == 0:
distincts -= 1
if right - left + 1 < len(p):
right += 1
else:
if distincts == 0:
res.append(left)
if s[left] in p_count:
p_count[s[left]] += 1
if p_count[s[left]] == 1:
distincts += 1
left += 1
right += 1
return res