最小滑动窗口,先找到满足条件的一个窗口,慢慢移动左指针找到符合条件的最小窗口。再移动右指针直到下一次符合条件的位置,再移动左指针。如此反复。例如
S = “ADOBECODEBANC”, T = “ABC”
- left=0
- right=5
- left=0,minL=6
- right=9
- left=0,minL=6
- right=10
- left=5,minL=6
- right=12
- left=9,minL=4
class Solution:
def minWindow(self, s, t):
"""
:type s: str
:type t: str
:rtype: str
"""
dict1={}
for c in t:
if c in dict1:
dict1[c]+=1
else:
dict1[c]=1
count,ls=len(t),len(s)
minL,curl=ls+1,0
resl=-1
for right in range(ls):
c1=s[right]
if c1 in dict1:
dict1[c1]-=1
if dict1[c1]>=0:
count-=1
if count==0:
while 1:
c2=s[curl]
if c2 in dict1:
if dict1[c2]<0:
dict1[c2]+=1
else:
break
curl+=1
if right-curl+1<minL:
minL= right-curl+1
resl=curl
return "" if resl==-1 else s[resl:resl+minL]