Fast Java Solution


  • 0
    public class Solution {
        public String minWindow(String s, String t) {
            if (s == null || s.length() == 0 || t == null || t.length() == 0) return "";
            int[] map = new int[256];
            for (char c : t.toCharArray()) {
                map[c]++;
            }
            int count = 0;
            int left = 0;
            int start = 0;
            int end = 0;
            int minLen = Integer.MAX_VALUE;
            for (int i = 0; i < s.length(); i++) {
                if (map[s.charAt(i)]-- > 0) {
                    count++;
                }
                while (count == t.length()) {
                    if (minLen > i - left + 1) {
                        minLen = i - left + 1;
                        start = left;
                        end = i;
                    }
                    if (++map[s.charAt(left++)] > 0) {
                        count--;
                    }
                }
            }
            return minLen == Integer.MAX_VALUE ? "" : s.substring(start, end + 1); 
        }
    }```

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.