Skip to content

Instantly share code, notes, and snippets.

@Siyu-Lei
Created May 29, 2018 04:17
Show Gist options
  • Save Siyu-Lei/9b6e7be0592d3d86bd3b0ae74295f9e9 to your computer and use it in GitHub Desktop.
Save Siyu-Lei/9b6e7be0592d3d86bd3b0ae74295f9e9 to your computer and use it in GitHub Desktop.
class Solution {
public String minWindow(String s, String t) {
int min = Integer.MAX_VALUE;
int minStart = 0;
int[] map = new int[128];
for (char c: t.toCharArray()) {
map[c]++;
}
int start = 0;
int end = 0;
int count = t.length();
while (end < s.length()) {
if (map[s.charAt(end)] > 0) {
count--;
}
map[s.charAt(end)]--;
end++;
while (count == 0) {
if (end - start < min) {
min = end - start;
minStart = start;
}
map[s.charAt(start)]++;
if (map[s.charAt(start)] > 0) {
count++;
}
start++;
}
}
return min == Integer.MAX_VALUE ? "" : s.substring(minStart, minStart + min);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment