描述

input S: ‘helloworld’ T: ‘dlr’output True or False
def checkInclusion(S: str, T: str) -> bool: need: dict[str:int] = {} window: dict[str:int] = {} flag: int = 0 for c in T: need[c] = need.get(c, 0) + 1 left: int = 0 right: int = 0 while right < len(S): c: str = S[right] right += 1 if c in T: window[c] = window.get(c, 0) + 1 if window[c] == need[c]: flag += 1 # 当flag满足条件时候, 目标串可能出现 while flag == len(need): if right - left == len(T): # [left, right], 这是当前窗口状态 return True c = S[left] if c in T: if window[c] == need[c]: flag -= 1 window[c] -= 1 left += 1 return Falseprint(checkInclusion('w321ww', '231'))