class Solution {
int[] counterS = new int[128];
int[] counterT = new int[128];
boolean check() {
for (int c = 'a'; c < 'z' + 1; c++) {
if (counterS[c] < counterT[c]) {
return false;
}
}
for (int c = 'A'; c < 'Z' + 1; c++) {
if (counterS[c] < counterT[c]) {
return false;
}
}
return true;
}
public String minWindow(String s, String t) {
if (s.length() < t.length()) {
return "";
}
for (int i = 0; i < t.length(); i++) {
counterT[t.charAt(i)]++;
}
int l = 0, r = t.length();
for (int i = 0; i < t.length(); i++) {
counterS[s.charAt(i)]++;
}
if (check()) {
return s.substring(l, t.length());
}
int MAX = 1000000, minLen = MAX, ml = 0;
while (r < s.length()) {
counterS[s.charAt(r)]++;
while (check() && l <= r) {
int tmpLen = r - l + 1;
if (tmpLen < minLen) {
minLen = tmpLen;
ml = l;
}
counterS[s.charAt(l++)]--;
}
r++;
}
if (minLen == MAX) {
return "";
}
return s.substring(ml, ml + minLen);
}
}
07-19
246
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交