Minimum Window Substring

两个指针,前一个后一个,移动后一个,直到包含所有的字符,移动前一个,直到不能移动,记下此时的最小窗口值
···
class Solution(object):
def minWindow(self, s, t):
"""
:type s: str
:type t: str
:rtype: str
"""
left = 0
minsize = len(s) + 1
minleft = 0
cmp1= {}
cmp2= {}
count = len(t)
for char in t:
if char not in cmp1:
cmp1[char] = 1
cmp2[char] = 1
else:
cmp1[char] += 1
cmp2[char] += 1

    for right in range(len(s)):
        if s[right] in cmp1:
            cmp2[s[right]] -=1
            if cmp2[s[right]] >= 0:
                count -= 1
            if count == 0:
                while True:
                    if s[left] in cmp2:
                        if cmp2[s[left]] < 0:
                            cmp2[s[left]] += 1
                        else:
                            break
                    left += 1
                if right - left + 1 < minsize:
                    minleft = left
                    minsize = right - minleft + 1
    if minsize < len(s) + 1:
        return s[minleft:minleft + minsize]
    else:
        return ''

···

你可能感兴趣的:(Minimum Window Substring)