-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathleetcode_0076_Minimum_Window_Substring.java
More file actions
71 lines (63 loc) · 2.43 KB
/
leetcode_0076_Minimum_Window_Substring.java
File metadata and controls
71 lines (63 loc) · 2.43 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
59
60
61
62
63
64
65
66
67
68
69
70
71
// AC: Runtime: 122 ms, faster than 5.02% of Java online submissions for Minimum Window Substring.
// Memory Usage: 39.9 MB, less than 32.04% of Java online submissions for Minimum Window Substring.
// sliding window
// T:O(26 * n + m) ~ O(m + n), S:O(26 * 2) ~ O(1)
//
class Solution {
public String minWindow(String s, String t) {
int sizeS = s.length(), sizeT = t.length(), minSize = Integer.MAX_VALUE, left = -1, right = -1;
if (sizeS < sizeT) {
return "";
}
HashMap<Character, Integer> recordS = new HashMap<>(), recordT = new HashMap<>();
for (char c : t.toCharArray()) {
recordT.merge(c, 1, Integer::sum);
}
for (char c : s.toCharArray()) {
recordS.merge(c, 1, Integer::sum);
}
for (char c : recordT.keySet()) {
if (!recordS.containsKey(c) || recordS.get(c) < recordT.get(c)) {
return "";
}
}
int leftPos = 0, rightPos = 0;
HashMap<Character, Integer> tempRecord = new HashMap<>();
tempRecord.merge(s.charAt(rightPos), 1, Integer::sum);
while (leftPos <= sizeS - 1) {
// right pointer forwarding
while (rightPos < sizeS - 1 && !check(tempRecord, recordT)) {
rightPos++;
tempRecord.merge(s.charAt(rightPos), 1, Integer::sum);
}
if (check(tempRecord, recordT)) {
if (rightPos - leftPos + 1 < minSize) {
minSize = rightPos - leftPos + 1;
left = leftPos;
right = rightPos;
}
} else {
break;
}
// left pointer forwarding
while (leftPos <= sizeS - 1 && check(tempRecord, recordT)) {
if (rightPos - leftPos + 1 < minSize) {
minSize = rightPos - leftPos + 1;
left = leftPos;
right = rightPos;
}
tempRecord.merge(s.charAt(leftPos), -1, Integer::sum);
leftPos++;
}
}
return left == -1 ? "" :s.substring(left, right + 1);
}
private boolean check(HashMap<Character, Integer> h1, HashMap<Character, Integer> h2) {
for (char c : h2.keySet()) {
if (!h1.containsKey(c) || h2.get(c) > h1.get(c)) {
return false;
}
}
return true;
}
}