-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumWindowSubstring.java
More file actions
50 lines (39 loc) · 1.32 KB
/
MinimumWindowSubstring.java
File metadata and controls
50 lines (39 loc) · 1.32 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
class Solution {
public String minWindow(String s, String t) {
if (t.length() == 0 || t.length() > s.length()) {
return "";
}
String result = "";
int[] chars = new int[128];
int[] t_chars = new int[128];
for (char c : t.toCharArray()) {
t_chars[c]++;
}
int l = 0;
int min_length = Integer.MAX_VALUE;
int requiredChars = t.length();
for(int r = 0; r < s.length(); r++){
if (t_chars[s.charAt(r)] > 0) {
requiredChars--;
}
t_chars[s.charAt(r)]--;
while (requiredChars == 0) {
if (r - l + 1 < min_length) {
min_length = r - l + 1;
result = s.substring(l, r + 1);
}
t_chars[s.charAt(l)]++;
if (t_chars[s.charAt(l)] > 0) {
requiredChars++;
}
l++;
}
}
return result;
}
}
/*
Approach: Sliding window moves to the right untill we find all required characters, and then we shrink left side untill we still have all necessary characters, thus with every iteration we can identify smallest window that has all necessary characters
Time Complexity: O (m + n)
Space Complexity: O (1)
*/