-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutation-in-string.py3
More file actions
43 lines (32 loc) · 1.01 KB
/
Copy pathpermutation-in-string.py3
File metadata and controls
43 lines (32 loc) · 1.01 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
# 567. Permutation in String (26/04/56418)
# Runtime: 52 ms (95.00%) Memory: 16.52 MB (86.02%)
# eidbaooo
# L
# R
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
# must have same character frequencies, if one is permutation of another
def charFrequencies(ss, data):
for s in ss:
if s in data:
data[s] += 1
else:
data[s] = 1
chars = {}
charFrequencies(s1, chars)
L = 0
R = len(s1)
charsSub = {}
for R in range(len(s2)):
if s2[R] in charsSub:
charsSub[s2[R]] += 1
else:
charsSub[s2[R]] = 1
if R - L + 1 == len(s1):
if charsSub == chars:
return True
charsSub[s2[L]] -= 1
if charsSub[s2[L]] == 0:
del charsSub[s2[L]]
L += 1
return False