class Solution {
    public String minWindow(String s, String t) {
        Map<Character,Integer> map = new HashMap<>();
        for (char c: t.toCharArray()) {
            map.put(c,map.getOrDefault(c,0)+1);
        }

        int start =0;
        int end = 0;
        int num = 0;
        int max = Integer.MAX_VALUE;
        int maxi =-1;
        int maxj = -1;
        int count = t.length();

        while (end < s.length()) {
            if (map.containsKey(s.charAt(end))) {
                if (map.get(s.charAt(end))> 0) count--;
                map.put(s.charAt(end),map.get(s.charAt(end))-1);
            }
            num++;

            while (count == 0) {
                if (map.containsKey(s.charAt(start))){
                    map.put(s.charAt(start),map.get(s.charAt(start))+1);
                    if (map.get(s.charAt(start)) > 0) count++;
                }

                num--;
                start++;
                if (count > 0 && num < max) {
                    max = num + 1;
                    maxi = start - 1;
                    maxj = end;
                }
            }


            end++;

        }
        return maxi !=-1 ? s.substring(maxi,maxj+1): "";
    }
}

results matching ""

    No results matching ""