> For the complete documentation index, see [llms.txt](https://grind75-notes.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://grind75-notes.gitbook.io/notes/week-8/minimum-window-substring.md).

# Minimum Window Substring

{% embed url="<https://leetcode.com/problems/minimum-window-substring>" %}

### Problem

> 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**.
>
> &#x20;
>
> **Example 1:**
>
> <pre data-overflow="wrap"><code>Input: s = "ADOBECODEBANC", t = "ABC"
> <strong>Output: "BANC"
> </strong><strong>Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.
> </strong></code></pre>
>
> **Example 2:**
>
> <pre><code>Input: s = "a", t = "a"
> <strong>Output: "a"
> </strong><strong>Explanation: The entire string s is the minimum window.
> </strong></code></pre>
>
> **Example 3:**
>
> <pre><code>Input: s = "a", t = "aa"
> <strong>Output: ""
> </strong><strong>Explanation:
> </strong>Both 'a's from t must be included in the window.
> Since the largest window of s only has one 'a', return empty string.
> </code></pre>

### Pseudocode

```
```

### Solution

```javascript
// from solutions
// https://leetcode.com/problems/minimum-window-substring/solutions/26808/here-is-a-10-line-template-that-can-solve-most-substring-problems/
var minWindow = function (s, t) {
  let counter = t.length;
  let shortestSubstring = "";
  const tMap = {};
  let start = 0;
  let end = 0;
  let minStart = 0;
  let minLen = Infinity;
  let size = s.length;

  for (let i = 0; i < t.length; i++) {
    if (!tMap[t[i]]) {
      tMap[t[i]] = 0;
    }
    tMap[t[i]] += 1;
  }

  while (end < size) {
    if (tMap[s[end]] > 0) {
      counter--;
    }

    tMap[s[end]]--;
    end++;
    console.log(tMap);
    while (counter === 0) {
      if (end - start < minLen) {
        minStart = start;
        minLen = end - start;
      }

      tMap[s[start]]++;
      console.log(tMap);
      if (tMap[s[start]] > 0) {
        counter++;
      }
      start++;
    }
  }
  console.log(tMap);
  if (minLen !== Infinity) {
    shortestSubstring = s.substring(minStart, minStart + minLen);
  }

  return shortestSubstring;
};
```

### Time and Space Complexity

#### Time

* What did the code do
* Total -

#### Space

* What did the code do
* Total -


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://grind75-notes.gitbook.io/notes/week-8/minimum-window-substring.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
