BiruLyu
5/23/2017 - 3:21 PM

76. Minimum Window Substring.java

class Solution {
    public String minWindow(String s, String t) {
        if (s == null || s.length() < 1 || t == null || t.length() < 1) return "";
        int end = 0, start = 0, count = t.length();
        int[] vector = new int[256];
        for (char c : t.toCharArray()) {
            vector[c]++;
        }
        char[] str = s.toCharArray();
        String res = "";
        int min = Integer.MAX_VALUE;
        while (end < str.length) {
            if (vector[str[end++]]-- > 0) count--;
            while (count == 0) {
                if (end - start < min) {
                    min = end - start;
                    res = s.substring(start, end);
                }
                if (vector[str[start++]]++ == 0) {
                    count++;
                }
            }
        }
        return res;
    }
}