-
Notifications
You must be signed in to change notification settings - Fork 0
/
Minimum Window Substring.java
41 lines (40 loc) · 1.14 KB
/
Minimum Window Substring.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
import java.util.HashMap;
class Solution {
public String minWindow(String s, String t) {
if (s == null || s.length() < t.length() || s.length() == 0)
return "";
HashMap<Character, Integer> map = new HashMap<>();
for (char c : t.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
int left = 0;
int minLen = s.length()+1;
int minLeft = 0;
int count = map.size();
for (int right = 0; right < s.length(); right++) {
if (map.containsKey(s.charAt(right))) {
map.put(s.charAt(right), map.get(s.charAt(right)) - 1);
if (map.get(s.charAt(right)) == 0)
count--;
}
System.out.println(count);
while (count == 0) {
if (right - left + 1 < minLen) {
minLeft = left;
minLen = right - left + 1;
}
if (map.containsKey(s.charAt(left))) {
map.put(s.charAt(left), map.get(s.charAt(left)) + 1);
if (map.get(s.charAt(left)) > 0) {
count++;
}
}
left++;
}
}
if (minLen > s.length()) {
return "";
}
return s.substring(minLeft, minLeft + minLen);
}
}