# 最小覆盖子串

<p>给你一个字符串 <code>s</code> 、一个字符串 <code>t</code> 。返回 <code>s</code> 中涵盖 <code>t</code> 所有字符的最小子串。如果 <code>s</code> 中不存在涵盖 <code>t</code> 所有字符的子串，则返回空字符串 <code>""</code> 。</p><p><strong>注意：</strong>如果 <code>s</code> 中存在这样的子串，我们保证它是唯一的答案。</p><p> </p><p><strong>示例 1：</strong></p><pre><strong>输入：</strong>s = "ADOBECODEBANC", t = "ABC"<strong><br />输出：</strong>"BANC"</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>s = "a", t = "a"<strong><br />输出：</strong>"a"</pre><p> </p><p><strong>提示：</strong></p><ul>	<li><code>1 <= s.length, t.length <= 10<sup>5</sup></code></li>	<li><code>s</code> 和 <code>t</code> 由英文字母组成</li></ul><p> </p><strong>进阶：</strong>你能设计一个在 <code>o(n)</code> 时间内解决此问题的算法吗？

以下程序实现了这一功能，请你填补空白处内容：

```python
class Solution(object):
    def minWindow(self, s, t):
        ls_s, ls_t = len(s), len(t)
        need_to_find = [0] * 256
        has_found = [0] * 256
        min_begin, min_end = 0, -1
        min_window = 100000000000000
        for index in range(ls_t):
            need_to_find[ord(t[index])] += 1
        count, begin = 0, 0
        for end in range(ls_s):
            end_index = ord(s[end])
            if need_to_find[end_index] == 0:
                continue
            has_found[end_index] += 1
            if has_found[end_index] <= need_to_find[end_index]:
                count += 1
            if count == ls_t:
                begin_index = ord(s[begin])
				____________________________;
                if window_ls < min_window:
                    min_begin = begin
                    min_end = end
                    min_window = window_ls
        if count == ls_t:
            return s[min_begin:min_end + 1]
        else:
            return ''


if __name__ == '__main__':
    s = Solution()
    print(s.minWindow('a', 'a'))
```

## template

```python
class Solution(object):
    def minWindow(self, s, t):
        ls_s, ls_t = len(s), len(t)
        need_to_find = [0] * 256
        has_found = [0] * 256
        min_begin, min_end = 0, -1
        min_window = 100000000000000
        for index in range(ls_t):
            need_to_find[ord(t[index])] += 1
        count, begin = 0, 0
        for end in range(ls_s):
            end_index = ord(s[end])
            if need_to_find[end_index] == 0:
                continue
            has_found[end_index] += 1
            if has_found[end_index] <= need_to_find[end_index]:
                count += 1
            if count == ls_t:
                begin_index = ord(s[begin])
                while need_to_find[begin_index] == 0 or\
                 has_found[begin_index] > need_to_find[begin_index]:
                    if has_found[begin_index] > need_to_find[begin_index]:
                        has_found[begin_index] -= 1
                    begin += 1
                    begin_index = ord(s[begin])
                window_ls = end - begin + 1
                if window_ls < min_window:
                    min_begin = begin
                    min_end = end
                    min_window = window_ls
        if count == ls_t:
            return s[min_begin:min_end + 1]
        else:
            return ''


if __name__ == '__main__':
    s = Solution()
    print(s.minWindow('a', 'a'))
```

## 答案

```python
while need_to_find[begin_index] == 0 or\
	has_found[begin_index] > need_to_find[begin_index]:
	if has_found[begin_index] > need_to_find[begin_index]:
		has_found[begin_index] -= 1
	begin += 1
	begin_index = ord(s[begin])
window_ls = end - begin + 1
```

## 选项

### A

```python
while need_to_find[begin_index] == 0 or\
	has_found[begin_index] > need_to_find[begin_index]:
	if has_found[begin_index] > need_to_find[begin_index]:
		has_found[begin_index] -= 1
	begin += 1
	begin_index = ord(s[begin])
window_ls = end - begin
```

### B

```python
while need_to_find[begin_index] == 0 or\
	has_found[begin_index] > need_to_find[begin_index]:
	if has_found[begin_index] > need_to_find[begin_index]:
		has_found[begin_index] += 1
	begin += 1
	begin_index = ord(s[begin])
window_ls = end - begin
```

### C

```python
while need_to_find[begin_index] == 0 or\
	has_found[begin_index] > need_to_find[begin_index]:
	if has_found[begin_index] > need_to_find[begin_index]:
		has_found[begin_index] += 1
	begin += 1
	begin_index = ord(s[begin])
window_ls = end - begin + 1
```