Sliding Window
经典题目
Two pointers + Map (char array)
public String minWindow(String s, String t) {
int[] dict = new int[128];
for (int i = 0; i < t.length(); i++) {
dict[t.charAt(i)] += 1;
}
int count = t.length();
int start = 0, end = 0;
int resStart = 0, resEnd = Integer.MAX_VALUE;
char[] array = s.toCharArray();
while (end < array.length) {
if (dict[array[end]] > 0) {
count -= 1;
}
dict[array[end]] -= 1;
end += 1;
while (count == 0) {
if (end - start - 1 < resEnd - resStart) {
resEnd = end - 1;
resStart = start;
}
dict[array[start]] += 1;
if (dict[array[start]] > 0) {
count += 1;
}
start += 1;
}
}
return resEnd == Integer.MAX_VALUE ? "" : s.substring(resStart, resEnd + 1);
}Last updated