Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 79ea466

Browse filesBrowse files
Create 76. 最小覆盖子串.md
1 parent de1b08d commit 79ea466
Copy full SHA for 79ea466

File tree

1 file changed

+88
-0
lines changed
Filter options

1 file changed

+88
-0
lines changed
+88Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#### 76. 最小覆盖子串
2+
3+
难度:困难
4+
5+
---
6+
7+
给你一个字符串 `s` 、一个字符串 `t` 。返回 `s` 中涵盖 `t` 所有字符的最小子串。如果 `s` 中不存在涵盖 `t` 所有字符的子串,则返回空字符串 `""`
8+
9+
**注意:**
10+
11+
* 对于 `t` 中重复字符,我们寻找的子字符串中该字符数量必须不少于 `t` 中该字符数量。
12+
* 如果 `s` 中存在这样的子串,我们保证它是唯一的答案。
13+
14+
**示例 1:**
15+
16+
```
17+
输入:s = "ADOBECODEBANC", t = "ABC"
18+
输出:"BANC"
19+
解释:最小覆盖子串 "BANC" 包含来自字符串 t 的 'A'、'B' 和 'C'。
20+
```
21+
22+
**示例 2:**
23+
24+
```
25+
输入:s = "a", t = "a"
26+
输出:"a"
27+
解释:整个字符串 s 是最小覆盖子串。
28+
```
29+
30+
**示例 3:**
31+
32+
```
33+
输入: s = "a", t = "aa"
34+
输出: ""
35+
解释: t 中两个字符 'a' 均应包含在 s 的子串中,
36+
因此没有符合条件的子字符串,返回空字符串。
37+
```
38+
39+
**提示:**
40+
41+
* `^m == s.length`
42+
* `^n == t.length`
43+
* `1 <= m, n <= 10^5`
44+
* `s``t` 由英文字母组成
45+
46+
**进阶:** 你能设计一个在 `o(m+n)` 时间内解决此问题的算法吗?
47+
48+
---
49+
50+
滑动窗口:
51+
52+
每次循环移动右移一次右指针,移动的过程中根据字符是否完全覆盖的情况循环右移左指针。第二个循环的过程中记录窗口长度是否为最小值。
53+
54+
```Go
55+
func minWindow(s string, t string) string {
56+
alphabet, count := map[byte]int{}, map[byte]int{}
57+
for i := range t {
58+
alphabet[t[i]]++
59+
}
60+
ansLeft, ansRight := -1, -1
61+
ansLen := math.MaxInt32
62+
for left, right := 0, 0; right < len(s); right++ {
63+
count[s[right]]++
64+
for check(alphabet, count) && left <= right {
65+
if right - left + 1 < ansLen {
66+
ansLen = right - left + 1
67+
ansLeft = left
68+
ansRight = right + 1
69+
}
70+
count[s[left]]--
71+
left++
72+
}
73+
}
74+
if ansLeft == -1 {
75+
return ""
76+
}
77+
return s[ansLeft:ansRight]
78+
}
79+
80+
func check(alphabet, count map[byte]int) bool {
81+
for k, v := range alphabet {
82+
if count[k] < v {
83+
return false
84+
}
85+
}
86+
return true
87+
}
88+
```

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.