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

# 0076. Minimum Window Substring

<https://leetcode.com/problems/minimum-window-substring>

## 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**.

A **substring** is a contiguous sequence of characters within the string.

**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?

## ac1: two pointers

```java
class Solution {
    public String minWindow(String s, String t) {
        // edge cases
        if (s == null || t == null) return "";

        // record char and counts
        int[] note = new int[128];
        for (int i = 0; i < t.length(); i++) {
            note[t.charAt(i)]++;
        }

        // walk original string
        int f = 0, cnt = t.length(), minlen = Integer.MAX_VALUE;
        for (int h = 0, e = 0; e < s.length(); e++) {
            // meet char, decrease 1 in note
            if (note[s.charAt(e)]-- > 0) cnt--;

            // cnt == 0, find substring, do sth
            while (cnt == 0) {
                // record length
                if (minlen > e - h + 1) {
                    minlen = e - h + 1;
                    f = h;
                }

                // move f forward, slide window
                if (h >= e) break;
                note[s.charAt(h)]++;
                if (note[s.charAt(h)] > 0) cnt++; // window lack one char, not qualified, move e forward to find a new window.
                h++;
            }
        }

        // result
        return minlen == Integer.MAX_VALUE ? "" : s.substring(f, f + minlen);
    }
}

/*
substring problem

1. record char and counts
int[] note = new int[128];
walk through target string

2. walk original string, meet char, decrease 1 in note
int f = 0, e = 0; // 2 pointers
int cnt = t.length();
when note[char] == 0, cnt--;

3. if cnt == 0, find target substring in original string. record length

4. move f forward, note[char]++

*/
```


---

# 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://jaywin.gitbook.io/leetcode/solutions/0076-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.
