# @lc code=start classSolution: defcheckInclusion(self, s1: str, s2: str) -> bool: needs = collections.defaultdict(int) for char in s1: needs[char] += 1 n1, n2 = len(s1), len(s2) if n1>n2: returnFalse
for i inrange(0, n2-n1+1): match = 0 window = collections.defaultdict(int) for c1 in s2[i:i+n1]: if c1 in needs: window[c1] += 1 if window[c1] == needs[c1]: match += 1 else: break if match== len(needs): returnTrue i += 1 returnFalse # @lc code=end
classSolution: defcheckInclusion(self, s1: str, s2: str) -> bool: needs = collections.defaultdict(int) window = collections.defaultdict(int) for char in s1: needs[char] += 1 n1, n2 = len(s1), len(s2) if n1>n2: returnFalse
match = 0 left, right = 0, 0 while right < n2: c1 = s2[right] if c1 in needs: window[c1] += 1 if window[c1]==needs[c1]: match += 1 right += 1
while match==len(needs): if right-left==len(s1): returnTrue c2 = s2[left] if c2 in needs: window[c2] -= 1 if window[c2] < needs[c2]: match -= 1 left += 1