-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermutation_in_string.py
61 lines (46 loc) · 1.35 KB
/
permutation_in_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
import collections
def test_1():
s1 = "ab"
s2 = "eidbaooo"
assert Solution().checkInclusion(s1, s2)
def test_2():
s1 = "ab"
s2 = "eidboaoo"
assert not Solution().checkInclusion(s1, s2)
def test_3():
s1 = "ab"
s2 = "a"
assert not Solution().checkInclusion(s1, s2)
def encode_str(s: str) -> collections.defaultdict:
enc = collections.defaultdict(int)
for c in s:
enc[c] += 1
return enc
def is_inclusion(example: dict, target: dict) -> bool:
for char, count in example.items():
if count > 0:
if char not in target or count != target[char]:
return False
return True
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
"""
Смотри пояснения в ./hash_table/find_all_anagrams_in_a_string.py
"""
if len(s1) > len(s2):
return False
l, r = 0, len(s1)
window_enc = encode_str(s2[l:r])
target_enc = encode_str(s1)
while r < len(s2):
if is_inclusion(window_enc, target_enc):
return True
l_char = s2[l]
r_char = s2[r]
window_enc[l_char] -= 1
window_enc[r_char] += 1
l += 1
r += 1
if is_inclusion(window_enc, target_enc):
return True
return False