forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimum Window Substring.java
More file actions
43 lines (36 loc) · 1.16 KB
/
Minimum Window Substring.java
File metadata and controls
43 lines (36 loc) · 1.16 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
class Solution {
public String minWindow(String s, String t) {
Map<Character, Integer> map = new HashMap<>();
for (char c : t.toCharArray()) {
map.put(c, (map.getOrDefault(c, 0) + 1));
}
int start = 0;
int end = 0;
int count = map.size();
int minLen = Integer.MAX_VALUE;
String ans = "";
while (end < s.length()) {
if (map.containsKey(s.charAt(end))) {
map.put(s.charAt(end), map.get(s.charAt(end)) - 1);
if (map.get(s.charAt(end)) == 0) {
count--;
}
}
end++;
while (count == 0) {
if (end - start < minLen) {
minLen = end - start;
ans = s.substring(start, end);
}
if (map.containsKey(s.charAt(start))) {
map.put(s.charAt(start), map.get(s.charAt(start)) + 1);
if (map.get(s.charAt(start)) > 0) {
count++;
}
}
start++;
}
}
return ans;
}
}