0076-minimum-window-substring

Try it on leetcode

Description

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.

 

Example 1:

Input: s = "ADOBECODEBANC", t = "ABC"
Output: "BANC"
Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.

Example 2:

Input: s = "a", t = "a"
Output: "a"
Explanation: The entire string s is the minimum window.

Example 3:

Input: s = "a", t = "aa"
Output: ""
Explanation: Both 'a's from t must be included in the window.
Since the largest window of s only has one 'a', return empty string.

 

Constraints:

  • m == s.length
  • n == t.length
  • 1 <= m, n <= 105
  • s and t consist of uppercase and lowercase English letters.

 

Follow up: Could you find an algorithm that runs in O(m + n) time?

Solution(Python)

from collections import Counter, defaultdict
class Solution:
    def minWindow(self, s: str, t: str) -> str:
        # s = "ADOBECODEBANC", t = "ABC"
        # minimum window substring with duplicates
        #  s = "a", t = "a"  ;a
        #   s = "a", t = "aa" ;""
        #  s = "ADOBECODEBANC", t = "ABC";  "BANC"
        #  s t 
        #   m n
        # case
        #  
        #  #1 n > m not valid return ""
        #  #2 m >= n maybe valid
        #    slide over s with window
        #    t = frequency of chars
        #    cur_window  = copy t frequency
        # 
        #    s = "ADOBECODEBANC", t = "ABC"
        #    m = 14
        #   n = 3
        #   t_frequency = {A: 1, B:1, C: 1}
        #  required = len(t_frequency)
        #   l = 0 r = 0 
        #    formed = 0
        #     check   A in t_frequency
        #       cur_window = {A: 1, B:1, C: 1}
        #       cur_window
        # Time Complexity :O(m+n)
        #  Space Complexity :O(m+n)

        if not t or not s:
            return ""

        t_freq = Counter(t)

        req = len(t_freq)

        filtered_s = []
        for i, char in enumerate(s):
            if char in t_freq:
                filtered_s.append((i, char))

        l, r= 0,0
        cur = 0

        window_cnts = defaultdict(int)

        ans = (float("inf"), None, None)

        while r < len(filtered_s):
            char = filtered_s[r][1]
            window_cnts[char] += 1

            if char in t_freq and  window_cnts[char] == t_freq[char]:
                cur  += 1
            
            while l <= r and cur == req:
                char  = filtered_s[l][1]

                end = filtered_s[r][0]
                start = filtered_s[l][0]
                if end - start + 1 < ans[0]:
                    ans = (end - start + 1, start, end)

                window_cnts[char] -= 1

                if char in t_freq and  window_cnts[char] < t_freq[char]:
                    cur  -= 1
                l += 1
            r += 1


        return "" if ans[0] == float("inf") else s[ans[1]: ans[2] + 1]