-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
438_Find_All_Anagrams_in_a_String.py
68 lines (66 loc) · 2.3 KB
/
438_Find_All_Anagrams_in_a_String.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
59
60
61
62
63
64
65
66
67
68
class Solution(object):
def findAnagrams(self, s, p):
"""
:type s: str
:type p: str
:rtype: List[int]
"""
res = []
if s is None or p is None or len(s) == 0 or len(p) == 0:
return res
char_map = [0] * 256
for c in p:
char_map[ord(c)] += 1
left, right, count = 0, 0, len(p)
while right < len(s):
if char_map[ord(s[right])] >= 1:
count -= 1
char_map[ord(s[right])] -= 1
right += 1
if count == 0:
res.append(left)
if right - left == len(p):
if char_map[ord(s[left])] >= 0:
count += 1
char_map[ord(s[left])] += 1
left += 1
return res
# def findAnagrams(self, s, p):
# if len(s) < len(p):
# return []
# res = []
# p_len = len(p)
# bit_map = []
# for _ in range(26):
# bit_map.append(0)
# for c in p:
# bit_map[ord(c) - ord('a')] += 1
# s_p = str(bit_map)
# for i in range(26):
# bit_map[i] = 0
# for i in range(p_len - 1):
# bit_map[ord(s[i]) - ord('a')] += 1
# for i in range(p_len - 1, len(s)):
# bit_map[ord(s[i]) - ord('a')] += 1
# if i - p_len >= 0:
# bit_map[ord(s[i - p_len]) - ord('a')] -= 1
# if str(bit_map) == s_p:
# res.append(i - p_len + 1)
# return res
# def findAnagrams(self, s, p):
# """
# :type s: str
# :type p: str
# :rtype: List[int]
# """
# res = []
# pCounter = collections.Counter(p)
# sCounter = collections.Counter(s[:len(p)-1])
# for i in range(len(p)-1,len(s)):
# sCounter[s[i]] += 1 # include a new char in the char_map
# if sCounter == pCounter: # This step is O(1), since there are at most 26 English letters
# res.append(i-len(p)+1) # append the starting index
# sCounter[s[i-len(p)+1]] -= 1 # decrease the count of oldest char in the window
# if sCounter[s[i-len(p)+1]] == 0:
# del sCounter[s[i-len(p)+1]] # remove the count if it is 0
# return res