</>

76. Minimum Window Substring

Hard NeetCode 150
Sliding Window Time O(n) Space O(1)
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
Sliding Window
Time
O(n)
Space
O(1)

Standard Solution (C++)

SummaryGiven two strings `s` and `t`, find the shortest contiguous substring of `s` that contains every character of `t` (with multiplicity), returning an empty string if no such window exists.

Approach
  • Build a frequency table for characters in t, and track how many distinct characters still need to be fully satisfied (need).
  • Expand the right pointer character by character; when a character's count in the window satisfies its required frequency, decrement need.
  • Once need == 0 (all characters satisfied), shrink the left pointer greedily: record the window if it is the smallest seen, then release the left character and, if that makes its count go below the required amount, increment need to exit the inner loop.
  • Because each character is added and removed at most once, the whole scan is linear.
#include <string>
#include <climits>
using namespace std;

class Solution {
public:
    string minWindow(string s, string t) {
        if (s.empty() || t.empty() || s.size() < t.size()) return "";

        // freq[c] > 0  → still need more of c
        // freq[c] <= 0 → window already satisfies c (possibly excess)
        int freq[128] = {};
        for (char c : t) ++freq[(unsigned char)c];

        // count of distinct characters whose requirement is not yet met
        int need = 0;
        for (int f : freq) if (f > 0) ++need;

        int left = 0, bestStart = -1, bestLen = INT_MAX;

        for (int right = 0; right < (int)s.size(); ++right) {
            // include s[right] in window
            char rc = s[right];
            --freq[(unsigned char)rc];
            if (freq[(unsigned char)rc] == 0) --need; // this character is now exactly satisfied

            // shrink from left while all requirements are met
            while (need == 0) {
                int windowLen = right - left + 1;
                if (windowLen < bestLen) {
                    bestLen  = windowLen;
                    bestStart = left;
                }

                // release s[left]
                char lc = s[left++];
                if (++freq[(unsigned char)lc] > 0) ++need; // we just lost a required character
            }
        }

        return bestStart == -1 ? "" : s.substr(bestStart, bestLen);
    }
};
Time O(n) Space O(1)

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

class Solution {
public:
    string minWindow(string s, string t) {
        int len1=s.length();
        int len2=t.length();

        if(len1<len2){
            return "";
        }
        
        // record frequency in t
        // unordered_map<char, int> umap;
        vector<int> umap(256, 0);
        for(const char& c: t){
            ++umap[c];
        }
        // int matchCount=umap.size();
        int matchCount=0;
        for(const int& i: umap){
            if(i!=0){
                ++matchCount;
            }
        }

        // sliding window
        int left=0;
        int right=0;
        int curMinWindow=INT_MAX;
        int startIdx=0;
        
        while(right<len1){ 
            --umap[s[right]];
            if(umap[s[right]]==0){
                --matchCount;
            }

            while(matchCount==0){ // move left pointer
                if(right-left+1<curMinWindow){
                    curMinWindow=right-left+1;
                    startIdx=left;
                }
                if(++umap[s[left]]>0){
                    ++matchCount;
                }
                ++left;
            }

            ++right;
        }

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

        return s.substr(startIdx, curMinWindow);
    }
};

// Time Complexity: O(n)
// Space Complexity: O(1)
Time O(n) Space O(1)

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern