Python


  • 0
    B
    class Solution(object):
        def minWindow(self, s, t):
            """
            :type s: str
            :type t: str
            :rtype: str
            """
            if len(s) == 0 or len(t) > len(s): return ""
            hashmap = {x:0 for x in t}
            hashmap1 = {}
            for v in t:
                if v not in hashmap1:
                    hashmap1[v] = 1
                else:
                    hashmap1[v] += 1
            left = right = 0
            cnt = len(hashmap1)
            retVal = ""
            while left < len(s):
                while cnt > 0 and right < len(s):
                    if s[right] in hashmap:
                        hashmap[s[right]] += 1
                        if hashmap[s[right]] == hashmap1[s[right]]:
                            cnt -= 1
                    right += 1
                if not retVal and cnt == 0:
                    retVal = s[left:right]
                if len(retVal) > right-left and cnt == 0:
                    retVal = s[left:right]
                if s[left] in hashmap: 
                    if hashmap[s[left]] == hashmap1[s[left]]:
                        cnt += 1
                    hashmap[s[left]] -= 1
                left += 1
            return retVal
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.