forked from wuduhren/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutation-in-string.py
More file actions
28 lines (24 loc) · 931 Bytes
/
permutation-in-string.py
File metadata and controls
28 lines (24 loc) · 931 Bytes
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
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
if len(s1)>len(s2): return False
counter1 = collections.Counter(s1)
counter2 = collections.Counter(s2[:len(s1)])
matches = 0
for c in 'abcdefghijklmnopqrstuvwxyz':
if counter1[c]==counter2[c]: matches += 1
if matches==26: return True
l = 0
for r in range(len(s1), len(s2)):
counter2[s2[r]] += 1
if counter1[s2[r]]==counter2[s2[r]]:
matches += 1
elif counter1[s2[r]]+1==counter2[s2[r]]:
matches -= 1
counter2[s2[l]] -= 1
if counter1[s2[l]]==counter2[s2[l]]:
matches += 1
elif counter1[s2[l]]-1==counter2[s2[l]]:
matches -= 1
l += 1
if matches==26: return True
return False