-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path76.minimum-window-substring.java
More file actions
60 lines (57 loc) · 1.97 KB
/
76.minimum-window-substring.java
File metadata and controls
60 lines (57 loc) · 1.97 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
/*
* @lc app=leetcode id=76 lang=java
*
* [76] Minimum Window Substring
*/
// @lc code=start
import java.util.*;
class Solution {
public String minWindow(String s, String t) {
if (t.equals(""))
return "";
int left = 0;
int[] res = new int[] { -1, -1 };
HashMap<Character, Integer> window = new HashMap<>();
HashMap<Character, Integer> conditions = new HashMap<>();
for (int i = 0; i < t.length(); i++) {
// -- method 1 --
// if (conditions.containsKey(t.charAt(i))) {
// conditions.put(t.charAt(i), conditions.get(t.charAt(i)) + 1);
// } else {
// conditions.put(t.charAt(i), 1);
// }
// -- method 2 --
// conditions.put(t.charAt(i), conditions.getOrDefault(t.charAt(i), 0) + 1);
// -- method 3 --
conditions.merge(t.charAt(i), 1, Integer::sum);
window.put(t.charAt(i), 0);
}
int have = 0, need = conditions.size();
for (int i = 0; i < s.length(); i++) {
char current = s.charAt(i);
if (conditions.containsKey(current)) {
window.merge(current, 1, Integer::sum);
if (window.get(current).equals(conditions.get(current))) {
have += 1;
}
}
while (have == need) {
if ((res[1] - res[0]) > (i - left) || res[0] == -1) {
res[0] = left;
res[1] = i;
}
if (conditions.containsKey(s.charAt(left))) {
window.merge(s.charAt(left), -1, Integer::sum);
if (window.get(s.charAt(left)) < conditions.get(s.charAt(left))) {
have -= 1;
}
}
left++;
}
}
if (res[0] == -1)
return "";
return s.substring(res[0], res[1] + 1);
}
}
// @lc code=end