76. Minimum Window Substring

Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string "".

The testcases will be generated such that the answer is unique.



Note: Hard
     Once missingCount is never negative.


def minWindow(self, s: str, t: str) -> str:
    need, missingCount = collections.Counter(t), len(t)
    i = I = J = 0
    for j, c in enumerate(s, 1):
        missingCount -= need[c] > 0            
        need[c] -= 1
        if missingCount == 0:
            while i < j and need[s[i]] < 0:
                need[s[i]] += 1
                i += 1
            if not J or j - i <= J - I:
                I, J = i, j

    return s[I:J] 

Comments

Popular posts from this blog

347. Top K Frequent Elements

849. Maximize Distance to Closest Person

674. Longest Continuous Increasing Subsequence