-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay 161.java
More file actions
46 lines (35 loc) · 1.17 KB
/
Day 161.java
File metadata and controls
46 lines (35 loc) · 1.17 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
class Solution {
public static String minWindow(String s, String p) {
if (p.length() > s.length()) return "";
int[] freq = new int[26];
for (char c : p.toCharArray()) {
freq[c - 'a']++;
}
int left = 0, right = 0;
int count = 0;
int minLen = Integer.MAX_VALUE;
int start = 0;
while (right < s.length()) {
char r = s.charAt(right);
if (freq[r - 'a'] > 0) {
count++;
}
freq[r - 'a']--;
right++;
while (count == p.length()) {
if (right - left < minLen) {
minLen = right - left;
start = left;
}
char l = s.charAt(left);
freq[l - 'a']++;
if (freq[l - 'a'] > 0) {
count--;
}
left++;
}
}
if (minLen == Integer.MAX_VALUE) return "";
return s.substring(start, start + minLen);
}
}