-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumWindowSubstring.java
More file actions
75 lines (61 loc) · 2.26 KB
/
MinimumWindowSubstring.java
File metadata and controls
75 lines (61 loc) · 2.26 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package leetcode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javafx.util.Pair;
public class MinimumWindowSubstring {
public String minWindow(String s, String t) {
if (s.length() == 0 || t.length() == 0 || t.length() > s.length()) {
return "";
}
Map<Character, Integer> dictT = new HashMap<>();
for (int i = 0; i < t.length(); i++) {
int count = dictT.getOrDefault(t.charAt(i), 0);
dictT.put(t.charAt(i), count + 1);
}
int required = dictT.size();
// Filter all the characters from s into a new list along with their index.
// The filtering criteria is that the character should be present in t.
List<Pair<Integer, Character>> filteredS = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (dictT.containsKey(c)) {
filteredS.add(new Pair<>(i, c));
}
}
int left = 0, right = 0, formed = 0;
Map<Character, Integer> windowCounts = new HashMap<>();
int[] ans = {-1, 0, 0};
// Look for the characters only in the filtered list instead of entire s.
// This helps to reduce our search.
// Hence, we follow the sliding window approach on as small list.
while (right < filteredS.size()) {
char c = filteredS.get(right).getValue();
int count = windowCounts.getOrDefault(c, 0);
windowCounts.put(c, count + 1);
if (dictT.containsKey(c) && windowCounts.get(c).intValue() == dictT.get(c).intValue()) {
formed++;
}
// Try and contract the window till the point where it ceases to be 'desirable'.
while (left <= right && formed == required) {
c = filteredS.get(left).getValue();
// Save the smallest window until now.
int end = filteredS.get(right).getKey();
int start = filteredS.get(left).getKey();
if (ans[0] == -1 || end - start + 1 < ans[0]) {
ans[0] = end - start + 1;
ans[1] = start;
ans[2] = end;
}
windowCounts.put(c, windowCounts.get(c) - 1);
if (dictT.containsKey(c) && windowCounts.get(c) < dictT.get(c)) {
formed--;
}
left++;
}
right++;
}
return ans[0] == -1 ? "" : s.substring(ans[1], ans[2] + 1);
}
}