-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathMinimumWindowSubstring2.java
53 lines (36 loc) · 1.39 KB
/
MinimumWindowSubstring2.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
class Solution {
public String findSubString( String str) {
HashSet<Character> set = new HashSet<>();
for(int i=0; i<str.length(); i++) {
set.add(str.charAt(i));
}
HashMap<Character, Integer> map = new HashMap<>();
int i = -1, j = -1;
String ans = "";
while(true) {
boolean f1 = false, f2 = false;
// Acquire
while(i<str.length()-1 && map.size() < set.size()) {
i++;
char ch = str.charAt(i);
map.put(ch, map.getOrDefault(ch,0) + 1);
f1 = true;
}
// Answer + Remove
while(j < i && map.size() == set.size()) {
String curAns = str.substring(j+1, i+1);
if(ans.length() == 0 || curAns.length() < ans.length()) ans = curAns;
j++;
char ch = str.charAt(j);
if(map.get(ch) == 1) {
map.remove(ch);
} else {
map.put(ch, map.get(ch)-1);
}
f2 = true;
}
if(!f1 && !f2) break;
}
return ans;
}
}