Leetcode567-字符串的排列
思路:滑动窗口。计数方法。
如果当前子串不满足条件,则向右移动。(left-1,right+1)
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
l1, l2 = len(s1), len(s2)
if l1 > l2:
return False
counter1 = {i: 0 for i in ascii_lowercase}
counter2 = {i: 0 for i in ascii_lowercase}
start = 0
end = l1
for i in s1:
counter1[i] += 1
for j in range(l1):
counter2[s2[j]] += 1
if counter1 == counter2:
return True
while end < l2:
counter2[s2[end]] += 1
counter2[s2[start]] -= 1
if counter1 == counter2:
return True
start += 1
end += 1
return False