Minimum Window Substring

Difficulty: Hard

Given strings s and t, find the shortest contiguous substring of s that contains every character of t, with counts (if t has two "a"s, the substring needs at least two).

Return that substring, or an empty string if none exists. If several tie, return any one.

Examples

Input: s = "ADOBECODEBANC", t = "ABC"
Output: "BANC"

"BANC" contains one A, one B, and one C — the minimum-length window in s that covers all of "ABC".

Input: s = "a", t = "a"
Output: "a"

The whole string already matches exactly.

Input: s = "a", t = "aa"
Output: ""

t needs two a's, but s only has one, so no valid window exists.

Constraints

  • 1 <= s.length, t.length <= 10^5

  • s and t consist of English letters (upper and/or lower case).

  • There is guaranteed to be at least one valid answer, or the correct result is an empty string.

Approach

Brute force checks every substring of s against t's requirements. That re-scans overlapping substrings and does a full comparison each time.

A sliding window is far faster. Count what t requires and what the window has. Grow the window from the right. When a character's count reaches exactly what t needs, one more requirement is "satisfied."

Once all requirements are satisfied, the window is valid. Shrink it from the left as far as possible, recording each new best, until a requirement drops below what's needed. Then grow from the right again.

The single "satisfied" counter avoids rechecking every count, and each character enters and leaves the window at most once.

Solutions

Brute Force — Check Every Substring

For every start and end position, build the substring's counts and check whether it covers t, tracking the shortest that works.

function minWindow(s, t) {
  if (t.length > s.length) return "";

  const need = buildCounts(t);
  let best = "";

  for (let start = 0; start < s.length; start++) {
    const have = {};
    for (let end = start; end < s.length; end++) {
      have[s[end]] = (have[s[end]] || 0) + 1;

      if (covers(need, have)) {
        const candidate = s.slice(start, end + 1);
        if (best === "" || candidate.length < best.length) {
          best = candidate;
        }
        break; // no need to extend this start further once it's valid
      }
    }
  }

  return best;
}

function buildCounts(str) {
  const counts = {};
  for (const ch of str) counts[ch] = (counts[ch] || 0) + 1;
  return counts;
}

function covers(need, have) {
  for (const ch in need) {
    if ((have[ch] || 0) < need[ch]) return false;
  }
  return true;
}

Time: O(n² * k) — n² substrings in the worst case, k = distinct characters in t for each coverage check · Space: O(k) for the character count maps

Optimal — Sliding Window with a Satisfied-Count

Grow the window until every requirement from t is met, tracked with one counter. Then shrink from the left as far as possible, recording the best valid window, before growing again.

function minWindow(s, t) {
  if (t.length > s.length) return "";

  const need = {};
  for (const ch of t) need[ch] = (need[ch] || 0) + 1;
  const required = Object.keys(need).length;

  const have = {};
  let satisfied = 0;
  let left = 0;
  let bestLen = Infinity;
  let bestStart = 0;

  for (let right = 0; right < s.length; right++) {
    const ch = s[right];
    have[ch] = (have[ch] || 0) + 1;

    if (need[ch] !== undefined && have[ch] === need[ch]) {
      satisfied++;
    }

    while (satisfied === required) {
      if (right - left + 1 < bestLen) {
        bestLen = right - left + 1;
        bestStart = left;
      }

      const leftChar = s[left];
      have[leftChar]--;
      if (need[leftChar] !== undefined && have[leftChar] < need[leftChar]) {
        satisfied--;
      }
      left++;
    }
  }

  return bestLen === Infinity ? "" : s.slice(bestStart, bestStart + bestLen);
}

Time: O(n + m) — n = s.length, m = t.length; each index enters and leaves the window at most once · Space: O(k) — k = distinct characters in t