-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinWindow.java
More file actions
58 lines (52 loc) · 1.96 KB
/
Copy pathMinWindow.java
File metadata and controls
58 lines (52 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package LintCode;
import java.util.HashMap;
import java.util.Map;
/**
* @FileName: MinWindow.java
* @Description: 最小子串覆盖
* @Author: ABCpril
* @Date: 2021/12/06
*/
public class MinWindow {
public String minWindow(String source, String target) {
Map<Character, Integer> window = new HashMap<>();
Map<Character, Integer> need = new HashMap<>();
int matchCnt = 0;
for (char c : target.toCharArray()) {
need.put(c, need.getOrDefault(c, 0) + 1);
}
char[] sourceChars = source.toCharArray();
// 包含target的最短子串长度,与开始下标
int l = 0, r = -1, length = Integer.MAX_VALUE, startIndex = -1;
while (r < sourceChars.length - 1) {
++r;
char rightChar = sourceChars[r];
int newFreqR = window.getOrDefault(rightChar, 0) + 1;
window.put(rightChar, newFreqR);
if (need.containsKey(rightChar)) {
// ==保证了超出target所需个数时,matchCnt不变
if (newFreqR == need.get(rightChar)) {
++matchCnt;
}
}
// when window needs shrink (窗口合规时,尝试缩小左边界)
while (matchCnt == need.size()) {
// 在最开始进行判断,窗口一定满足包含target字符
if (r - l + 1 < length) {
length = r - l + 1;
startIndex = l;
}
char leftChar = sourceChars[l];
++l;
if (need.containsKey(leftChar)) {
int newFreqL = window.get(leftChar) - 1;
if (newFreqL < need.get(leftChar)) {
--matchCnt;
}
window.put(leftChar, newFreqL);
}
}
}
return length == Integer.MAX_VALUE ? "" : new String(sourceChars, startIndex, length);
}
}