Skip to content

Commit 8e7da3c

Browse files
committed
三刷76
1 parent 9116411 commit 8e7da3c

File tree

4 files changed

+116
-11
lines changed

4 files changed

+116
-11
lines changed

Diff for: docs/0076-minimum-window-substring.adoc

+60-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,49 @@
11
[#0076-minimum-window-substring]
2-
= 76. Minimum Window Substring
2+
= 76. 最小覆盖子串
33

4-
{leetcode}/problems/minimum-window-substring/[LeetCode - Minimum Window Substring^]
4+
https://leetcode.cn/problems/minimum-window-substring/[LeetCode - 76. 最小覆盖子串 ^]
55

6-
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
6+
给你一个字符串 `s` 、一个字符串 `t` 。返回 `s` 中涵盖 `t` 所有字符的最小子串。如果 `s` 中不存在涵盖 `t` 所有字符的子串,则返回空字符串 `""`
77

8-
.Example:
9-
----
10-
Input: S = "ADOBECODEBANC", T = "ABC"
11-
Output: "BANC"
12-
----
8+
*注意:*
9+
10+
* 对于 `t` 中重复字符,我们寻找的子字符串中该字符数量必须不少于 `t` 中该字符数量。
11+
* 如果 `s` 中存在这样的子串,我们保证它是唯一的答案。
12+
13+
*示例 1:*
14+
15+
....
16+
输入:s = "ADOBECODEBANC", t = "ABC"
17+
输出:"BANC"
18+
解释:最小覆盖子串 "BANC" 包含来自字符串 t 的 'A'、'B' 和 'C'。
19+
....
20+
21+
*示例 2:*
22+
23+
....
24+
输入:s = "a", t = "a"
25+
输出:"a"
26+
解释:整个字符串 s 是最小覆盖子串。
27+
....
28+
29+
*示例 3:*
1330

14-
*Note:*
31+
....
32+
输入: s = "a", t = "aa"
33+
输出: ""
34+
解释: t 中两个字符 'a' 均应包含在 s 的子串中,
35+
因此没有符合条件的子字符串,返回空字符串。
36+
....
1537

16-
* If there is no such window in S that covers all characters in T, return the empty string "".
17-
* If there is such window, you are guaranteed that there will always be only one unique minimum window in S.
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)` 时间内解决此问题的算法吗?
1847

1948
== 思路分析
2049

@@ -23,15 +52,35 @@ Output: "BANC"
2352
image::images/0076-01.gif[{image_attr}]
2453

2554
[[src-0076]]
55+
[tabs]
56+
====
57+
一刷::
58+
+
59+
--
2660
[{java_src_attr}]
2761
----
2862
include::{sourcedir}/_0076_MinimumWindowSubstring.java[tag=answer]
2963
----
64+
--
3065
66+
二刷::
67+
+
68+
--
3169
[{java_src_attr}]
3270
----
3371
include::{sourcedir}/_0076_MinimumWindowSubstring_2.java[tag=answer]
3472
----
73+
--
74+
75+
三刷::
76+
+
77+
--
78+
[{java_src_attr}]
79+
----
80+
include::{sourcedir}/_0076_MinimumWindowSubstring_3.java[tag=answer]
81+
----
82+
--
83+
====
3584

3685
== 参考资料
3786

Diff for: logbook/202503.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ endif::[]
5555
|{doc_base_url}/0202-happy-number.adoc[题解]
5656
|✅ 快慢指针
5757

58+
|{counter:codes2503}
59+
|{leetcode_base_url}/minimum-window-substring/[76. Minimum Window Substring^]
60+
|{doc_base_url}/0076-minimum-window-substring.adoc[题解]
61+
|⭕️ 滑动窗口,一定注意细节的处理。
62+
5863
|===
5964

6065
截止目前,本轮练习一共完成 {codes2503} 道题。

Diff for: src/main/java/com/diguage/algo/leetcode/_0076_MinimumWindowSubstring_2.java

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public class _0076_MinimumWindowSubstring_2 {
1616

1717
/**
1818
* 自己实现失败后,参考社区答案修改的
19+
*
20+
* @author D瓜哥 · https://www.diguage.com
21+
* @since 2024-07-02 17:03:23
1922
*/
2023
public String minWindow(String s, String t) {
2124
if (s == null || s.isEmpty() || t == null || t.isEmpty() || s.length() < t.length()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Objects;
6+
7+
public class _0076_MinimumWindowSubstring_3 {
8+
// tag::answer[]
9+
/**
10+
* @author D瓜哥 · https://www.diguage.com
11+
* @since 2025-03-21 11:08:35
12+
*/
13+
public String minWindow(String s, String t) {
14+
if (s == null || t == null || s.isEmpty() || t.isEmpty() || s.length() < t.length()) {
15+
return "";
16+
}
17+
Map<Character, Integer> target = new HashMap<>();
18+
for (char c : t.toCharArray()) {
19+
target.put(c, target.getOrDefault(c, 0) + 1);
20+
}
21+
int left = 0, right = 0;
22+
int valid = 0, startIdx = 0, minLength = Integer.MAX_VALUE;
23+
Map<Character, Integer> windows = new HashMap<>();
24+
while (right < s.length()) {
25+
char rc = s.charAt(right);
26+
right++;
27+
windows.put(rc, windows.getOrDefault(rc, 0) + 1);
28+
if (target.containsKey(rc)
29+
&& Objects.equals(target.get(rc), windows.get(rc))) {
30+
valid++;
31+
}
32+
while (valid == target.size()) {
33+
if (right - left < minLength) {
34+
minLength = right - left;
35+
startIdx = left;
36+
}
37+
char lc = s.charAt(left);
38+
windows.put(lc, windows.getOrDefault(lc, 0) - 1);
39+
if (target.containsKey(lc) && windows.get(lc) < target.get(lc)) {
40+
valid--;
41+
}
42+
left++;
43+
}
44+
}
45+
return minLength == Integer.MAX_VALUE ? "" : s.substring(startIdx, startIdx + minLength);
46+
}
47+
// end::answer[]
48+
}

0 commit comments

Comments
 (0)