forked from neetcode-gh/leetcode
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0567-permutation-in-string.py
34 lines (29 loc) · 1.02 KB
/
0567-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
classSolution:
defcheckInclusion(self, s1: str, s2: str) ->bool:
iflen(s1) >len(s2):
returnFalse
s1Count, s2Count= [0] *26, [0] *26
foriinrange(len(s1)):
s1Count[ord(s1[i]) -ord("a")] +=1
s2Count[ord(s2[i]) -ord("a")] +=1
matches=0
foriinrange(26):
matches+=1ifs1Count[i] ==s2Count[i] else0
l=0
forrinrange(len(s1), len(s2)):
ifmatches==26:
returnTrue
index=ord(s2[r]) -ord("a")
s2Count[index] +=1
ifs1Count[index] ==s2Count[index]:
matches+=1
elifs1Count[index] +1==s2Count[index]:
matches-=1
index=ord(s2[l]) -ord("a")
s2Count[index] -=1
ifs1Count[index] ==s2Count[index]:
matches+=1
elifs1Count[index] -1==s2Count[index]:
matches-=1
l+=1
returnmatches==26