# 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]++

*/
```
