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.

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.

Approach 01:

#include <bits/stdc++.h>

using namespace std;

class Solution {
public:
    string minWindow(string s, string t) {
        unordered_map<char, int> freq_t;
        int m = s.length();
        int n = t.length();

        if (n > m) {
            return "";
        }

        for(auto &ch: t){
            freq_t[ch]++;
        }

        int i = 0, j = 0;
        int required_cnt = n;
        int min_window_size = INT_MAX;
        int start_i = 0;

        while (j < m) {
            char ch = s[j];
            if (freq_t[ch] > 0) {
                required_cnt--;
            }
            freq_t[ch]--;
            while (required_cnt == 0) {
                int curr_window_size = j - i + 1;
                if (curr_window_size < min_window_size) {
                    min_window_size = curr_window_size;
                    start_i = i;
                }
                freq_t[s[i]]++;
                if (freq_t[s[i]] > 0) {
                    required_cnt++;
                }
                i++;
            }
            j++;
        }

        if (min_window_size == INT_MAX) {
            return "";
        }

        return s.substr(start_i, min_window_size);
    }
};
from typing import *
import collections
import sys

class Solution:
    def minWindow(self, s: str, t: str) -> str:
        freq_t = collections.Counter(t)
        m = len(s)
        n = len(t)
        
        if(n>m):
            return ''

        i, j = 0, 0
        required_cnt = n
        min_window_size = sys.maxsize
        start_i = 0

        while(j<m):
            ch = s[j]
            if(freq_t[ch] > 0):
                required_cnt -= 1
            freq_t[ch] -= 1
            while(required_cnt == 0):
                curr_window_size = j-i+1
                if(curr_window_size < min_window_size):
                    min_window_size = curr_window_size
                    start_i = i
                freq_t[s[i]] += 1
                if(freq_t[s[i]] > 0):
                    required_cnt += 1
                i += 1 
            j += 1
        
        if(min_window_size == sys.maxsize):
            return ''
        return s[start_i: start_i + min_window_size]

Time Complexity

  • The time complexity of the minWindow function is \( O(m + n) \), where \( m \) is the length of the string s and \( n \) is the length of the string t.
  • Building the frequency map freq_t for string t takes \( O(n) \) time.
  • The two-pointer technique to find the minimum window involves iterating through the string s using two pointers i and j, which takes \( O(m) \) time in total.
  • Thus, the overall time complexity is \( O(m) + O(n) = O(m + n) \).

Space Complexity

  • The space complexity of the minWindow function is \( O(k) \), where \( k \) is the number of unique characters in the string t.
  • The frequency map freq_t stores the count of each character in t, and the size of this map is proportional to the number of unique characters in t. Therefore, the space complexity for the map is \( O(k) \).
  • Additional space is used for a few integer variables, which does not depend on the input size, hence considered as \( O(1) \).
  • Thus, the overall space complexity is \( O(k) \).


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top