滑动窗口
python
import collections
class Solution:
def checkInclusion(self, s1: str, s2: str):
left, right = 0, 0
n = len(s2)
m = len(s1)
valid = 0
needs = collections.Counter(s1)
window = {}
while right < n:
c = s2[right]
right += 1
if needs.get(c, -1) != -1:
if window.get(c, -1) == -1:
window[c] = 1
else:
window[c] += 1
if window[c] == needs[c]:
valid += 1
while right - left == m:
if valid == len(needs):
return True
d = s2[left]
left += 1
if needs.get(d, -1) != -1:
if window[d] == needs[d]:
valid -= 1
window[d] -= 1
return False