-
Notifications
You must be signed in to change notification settings - Fork 21
/
h.py
48 lines (37 loc) · 1.26 KB
/
h.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
from collections import Counter
def maya(word, text):
word_len = len(word)
result = 0
word_letters_counter = Counter(word)
current_sliding_window_len = 0
matching_letters_count = 0
for i, c in enumerate(text):
if current_sliding_window_len >= word_len:
leftmost_char_out_of_current_sliding_window = text[i - word_len]
if word_letters_counter[leftmost_char_out_of_current_sliding_window] >= 0:
matching_letters_count -= 1
else:
matching_letters_count += 1
word_letters_counter[leftmost_char_out_of_current_sliding_window] += 1
else:
current_sliding_window_len += 1
if word_letters_counter[c] > 0:
matching_letters_count += 1
else:
matching_letters_count -= 1
word_letters_counter[c] -= 1
if matching_letters_count == word_len:
result += 1
return result
assert maya('cAda', 'AbrAcadAbRa') == 2
assert maya(
'OOO',
'OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO'
) == 98
def main():
g, s_len = map(int, input().split())
w = input()
s = input()
print(maya(w, s))
if __name__ == '__main__':
main()