forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathekgns33.java
More file actions
30 lines (30 loc) · 725 Bytes
/
ekgns33.java
File metadata and controls
30 lines (30 loc) · 725 Bytes
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
class Solution {
public String minWindow(String s, String t) {
int[] freq = new int[128];
char[] str = s.toCharArray();
int minL = 0;
int minLength = Integer.MAX_VALUE;
int l = 0, r = 0, n = s.length();
for(char c : t.toCharArray()) {
freq[c-'A']++;
}
int resolved = t.length();
while(r < n) {
if(freq[str[r++] - 'A']-- > 0) {
resolved--;
}
while(resolved == 0) {
if(r - l < minLength) {
minL = l;
minLength = r - l;
}
if(freq[str[l] - 'A']++ == 0) {
resolved++;
}
l++;
}
}
if(minLength == Integer.MAX_VALUE) return "";
return new String(str, minL, minLength);
}
}