https://leetcode.cn/problems/permutation-in-string/
给你两个字符串 s1 和 s2 ,写一个函数来判断 s2 是否包含 s1 的排列。如果是,返回 true ;否则,返回 false 。
换句话说,s1 的排列之一是 s2 的 子串 。
示例 1:
输入:s1 = “ab” s2 = “eidbaooo”
输出:true
解释:s2 包含 s1 的排列之一 (“ba”).
示例 2:
输入:s1= “ab” s2 = “eidboaoo”
输出:false
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
# 字符串、哈希表、滑动窗口
# 本题和438题思路完全一样,window定宽
from collections import Counter
need = Counter(s1)
left, right = 0, len(s1) - 1
while right < len(s2):
window = Counter(s2[left: right + 1])
if need == window:
return True
left += 1
right += 1
return False