-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetCode0076.java
64 lines (53 loc) · 1.86 KB
/
LeetCode0076.java
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
59
60
61
62
63
64
/* Minimum Window Substring
* Input: S = "ADOBECODEBANC", T = "ABC"
* Output: "BANC"
* */
import java.util.HashMap;
import java.util.Map;
public class LeetCode0076 {
public static void main(String args[]) {
String s = "ADOBECODEBANC";
String t = "AABC";
System.out.println(minWindow(s, t));
}
public static String minWindow(String s, String t) {
if (s.length() == 0 || t.length() == 0 || t.length() > s.length())
return "";
//统计t中的字母及其个数
Map<Character, Integer> map = new HashMap();
for (char c : t.toCharArray())
map.put(c, map.getOrDefault(c, 0) + 1);
int counter = map.size();
int begin = 0; //左指针
int end = 0; //右指针
int head = 0;
int length = Integer.MAX_VALUE;
while (end < s.length()) {
char c = s.charAt(end);
if (map.containsKey(c)) {
map.put(c, map.get(c) - 1); //该字母个数减一
if (map.get(c) == 0)
counter--; //匹配一个字母
}
end++;
//所有字母都匹配后调整窗口长度
while (counter == 0) {
char tempc = s.charAt(begin);
if (map.containsKey(tempc)) {
map.put(tempc, map.get(tempc) + 1);
if (map.get(tempc) > 0) {
counter++;
}
}
if (end - begin < length) {
length = end - begin;
head = begin;
}
begin++;
}
}
if (length == Integer.MAX_VALUE)
return "";
return s.substring(head, head + length);
}
}