8000 Add Solution in Python · qilingit/Leetcode@6e0c55a · GitHub
[go: up one dir, main page]

Skip to content

Commit 6e0c55a

Browse files
Add Solution in Python
1 parent fafaa3c commit 6e0c55a
Copy full SHA for 6e0c55a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def checkInclusion(self, s1: str, s2: str) -> bool:
3+
need = dict(collections.Counter(s1))
4+
window = {}
5+
left,right = 0,0
6+
while right < len(s2):
7+
letter = s2[right]
8+
if letter not in need:
9+
window.clear()
10+
left = right+1
11+
else:
12+
window[letter]=window.get(letter,0)+1
13+
if window==need:
14+
return True
15+
if window[letter]>need[letter]:
16+
while left<=right:
17+
if s2[left]!=letter:
18+
window[s2[left]] = window.get(s2[left],0)-1
19+
left+=1
20+
else:
21+
left+=1
22+
break
23+
window[letter]-=1
24+
right+=1
25+
return False

0 commit comments

Comments
 (0)
0