-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path567.py
More file actions
40 lines (35 loc) · 1.26 KB
/
567.py
File metadata and controls
40 lines (35 loc) · 1.26 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
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
# turn s1 into a dictionary
s1Dict = {}
for i in range(len(s1)):
if (s1[i] in s1Dict):
s1Dict[s1[i]] += 1
else:
s1Dict[s1[i]] = 1
s1Distance = len(s1) # distance away from s1 (sum of s1Dict)
# GAMEPLAN: subtract from s1Dict
p1 = 0
for p2 in range(len(s2)):
if (p2 < p1):
continue
if (s1Distance == 0):
return True
elif (s2[p2] in s1Dict):
if (s1Dict[s2[p2]] > 0):
s1Dict[s2[p2]] -= 1
s1Distance -= 1
else:
s1Dict[s2[p2]] -= 1
s1Distance -= 1
while (s1Dict[s2[p2]] < 0): # when 0, back to good
s1Dict[s2[p1]] += 1
s1Distance += 1
p1 += 1
else:
while (p1 < p2):
s1Dict[s2[p1]] += 1
s1Distance += 1
p1 += 1
p1 += 1 # avoid including current unused letter
return (s1Distance == 0)