forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_111.py
50 lines (35 loc) · 1.27 KB
/
problem_111.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
def add_needed_char(needed, char):
if char not in needed:
needed[char] = 0
needed[char] += 1
def remove_gained_char(needed, char):
needed[char] -= 1
def get_anagram_starts(string, word):
if len(word) > len(string):
return []
anagram_indices = list()
charset = set(word)
needed = dict()
for i, char_needed in enumerate(word):
add_needed_char(needed, char_needed)
for i in range(len(word)):
char_gained = string[i]
remove_gained_char(needed, char_gained)
# This is a constant time operation because it has
# a fixed upper bound of 26 read operations
if all([x < 1 for x in needed.values()]):
anagram_indices.append(0)
for i in range(len(word), len(string)):
window_start = i - len(word)
char_removed = string[window_start]
char_gained = string[i]
if char_removed in charset:
add_needed_char(needed, char_removed)
if char_gained in needed:
remove_gained_char(needed, char_gained)
if all([x < 1 for x in needed.values()]):
anagram_indices.append(window_start + 1)
return anagram_indices
# Tests
assert get_anagram_starts("abxaba", "ab") == [0, 3, 4]
assert get_anagram_starts("cataract", "tac") == [0, 5]