/
Minimum Window Substring.java
58 lines (55 loc) · 1.89 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public class Solution {
public String minWindow(String s, String t) {
if(s.length() == 0 || t.length() > s.length())
return "";
HashMap<Character, Integer> target = new HashMap<Character, Integer>();
char[] tArr = t.toCharArray();
for(char one : tArr) {
if(!target.containsKey(one))
target.put(one, 1);
else
target.put(one, target.get(one) + 1);
}
int len = s.length() + 1;
String res = "";
int pos = 0;
int prev = 0;
int count = 0;
HashMap<Character, Integer> m = new HashMap<Character, Integer> ();
while(pos < s.length()) {
char oneChar = s.charAt(pos);
if(target.containsKey(oneChar)) {
if(!m.containsKey(oneChar)) {
count++;
m.put(oneChar, 1);
}
else {
m.put(oneChar, m.get(oneChar) + 1);
if(m.get(oneChar) <= target.get(oneChar))
count++;
}
}
// move the prev to update len
if(count == t.length()) {
while(prev <= pos) {
char temp = s.charAt(prev);
if(!m.containsKey(temp))
prev++;
else if(m.get(temp) > target.get(temp)) {
prev++;
m.put(temp, m.get(temp) - 1);
}
else {
if(pos - prev + 1 < len) {
len = pos - prev + 1;
res = s.substring(prev, pos + 1);
}
break;
}
}
}
pos++;
}
return res;
}
}