-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path438.py
46 lines (38 loc) · 1.38 KB
/
438.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
# Runtime: 136 ms, faster than 53.95% of Python3 online submissions for Find All Anagrams in a String.
# Memory Usage: 14.9 MB, less than 8.70% of Python3 online submissions for Find All Anagrams in a String.
# Difficulty: Medium
class Solution:
def findAnagrams(self, s, p):
"""
:type s: str
:type p: str
:rtype: List[int]
"""
if len(p) > len(s):
return list()
p_chars = dict()
for char in p:
p_chars[char] = p_chars.get(char, 0) + 1
distinct = len(p_chars)
output = list()
for i in range(len(p)):
if s[i] in p_chars:
p_chars[s[i]] -= 1
if p_chars[s[i]] == 0:
distinct -= 1
if distinct == 0:
output.append(0)
index = 1
while index + len(p) <= len(s):
if s[index - 1] in p_chars:
p_chars[s[index - 1]] += 1
if p_chars[s[index - 1]] == 1:
distinct += 1
if s[index + len(p) - 1] in p_chars:
p_chars[s[index + len(p) - 1]] -= 1
if p_chars[s[index + len(p) - 1]] == 0:
distinct -= 1
if distinct == 0:
output.append(index)
index += 1
return output