public class Solution {
/**
*
* @param S string字符串
* @param T string字符串
* @return string字符串
*/
public String minWindow (String S, String T) {
// write code here
if (S == null || S.length() < 1 || T == null || T.length() < 1) {
return "";
}
char[] sArr = S.toCharArray();
char[] tArr = T.toCharArray();
int[] map = new int[128];
for (char ch : tArr) {
map[(int) ch]++;
}
int slow = 0;
int fast = 0;
int left = -1;
int right = -1;
int cnt = tArr.length;
int len = Integer.MAX_VALUE;
while (fast < sArr.length) {
char ch = sArr[fast];
int key = (int) ch;
if (map[key]-- > 0) {
cnt--;
}
while (cnt == 0) {
if (fast - slow + 1 < len) {
len = fast - slow + 1;
left = slow;
right = fast;
}
ch = sArr[slow];
key = (int) ch;
if (map[key]++ == 0) {
cnt++;
}
slow++;
}
fast++;
}
if (left == -1) {
return "";
}
return S.substring(left, right + 1);
}
}