-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminWindowSubstring.java
More file actions
54 lines (42 loc) · 1.37 KB
/
minWindowSubstring.java
File metadata and controls
54 lines (42 loc) · 1.37 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
class Solution {
public String minWindow(String s, String t) {
HashMap<Character,Integer> m = new HashMap<>();
for (Character c: t.toCharArray()) {
if (m.containsKey(c)){
m.put(c, m.get(c)+1);
}else{
m.put(c,1);
}
}
Integer count=t.length(), start=0, end=0, minStart=0, minLen = Integer.MAX_VALUE;
Integer size = s.length();
char[] s1 = s.toCharArray();
while(end < size){
if (m.containsKey(s1[end])){
if (m.get(s1[end]) > 0){
count--;
}
}
if (m.containsKey(s1[end])){
m.put(s1[end], m.get(s1[end]) -1);
}
end++;
while(count==0){
if (minLen > end-start){
minLen = end-start;
minStart = start;
}
if (m.containsKey(s1[start])) {
m.put(s1[start], m.get(s1[start])+1);
}
if (m.containsKey(s1[start])) {
if (m.get(s1[start]) > 0) {
count++;
}
}
start++;
}
}
return minLen==Integer.MAX_VALUE ? "" : s.substring(minStart, minStart+minLen);
}
}