Created
May 29, 2018 04:17
-
-
Save Siyu-Lei/9b6e7be0592d3d86bd3b0ae74295f9e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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