-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0567_Permutation_in_String.py
More file actions
50 lines (43 loc) · 1.56 KB
/
0567_Permutation_in_String.py
File metadata and controls
50 lines (43 loc) · 1.56 KB
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
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
if len(s1) > len(s2):
return False
s1_list_letter_freq = {}
for each_s1 in s1:
if each_s1 in s1_list_letter_freq:
s1_list_letter_freq[each_s1] += 1
else:
s1_list_letter_freq[each_s1] = 1
# init
right = len(s1)
curr_dict = {}
for index in range(right):
if s2[index] in curr_dict:
curr_dict[s2[index]] += 1
else:
curr_dict[s2[index]] = 1
def check_per(_curr_dict, _target):
for letter, freq in _target.items():
if not letter in _curr_dict:
return False
elif not freq == _curr_dict[letter]:
return False
return True
# init Check
if check_per(curr_dict, s1_list_letter_freq):
return True
# Rest
for index in range(right, len(s2)):
right_letter = s2[index]
if right_letter in curr_dict:
curr_dict[right_letter] += 1
else:
curr_dict[right_letter] = 1
earliest_letter = s2[index-len(s1)]
if curr_dict[earliest_letter] == 1:
del curr_dict[earliest_letter]
else:
curr_dict[earliest_letter] -= 1
if check_per(curr_dict, s1_list_letter_freq):
return True
return False