【LeetCode】【HOT】76. 最小覆盖子串

package hot;
public class Solution76 {
public static void main(String[] args) {
String s = "ADOBECODEBANC";
String t = "ABC";
Solution76 solution = new Solution76();
System.out.println(solution.method(s,t));
}
private String method(String s, String t){
if (s == null || s.length() == 0 || t == null || t.length() == 0){
return "";
}
int[] need = new int[128];
for (int i = 0; i < t.length(); i++) {
need[t.charAt(i)]++;
}
int size = Integer.MAX_VALUE;
int count = t.length();
int l = 0;
int r = 0;
int start = 0;
while (r < s.length()) {
char c = s.charAt(r);
if (need[c] > 0) {
count--;
}
need[c]--;
if (count == 0) {
while (l < r && need[s.charAt(l)] < 0) {
need[s.charAt(l)]++;
l++;
}
if (r - l + 1 < size) {
size = r - l + 1;
start = l;
}
need[s.charAt(l)]++;
l++;
count++;
}
r++;
}
return size == Integer.MAX_VALUE ? "" : s.substring(start, start + size);
}
}