76. Minimum Window Substring

Hard

Sliding window的典型题,LC Discuss里面给了模版讨论,需要再练几道找找感觉.

总体的sliding window思路

  1. Use two pointers: start and end to represent a window.
  2. Move end to find a valid window.
  3. When a valid window is found, move start to find a smaller window.

本题要先用一个hashMap来记录t里面每个character出现的频数. 初始化two pointers start, end == 0. count表示还需要在s里找到count个t中含有的character, 初始化为t.length(). minStart表示最后找到的minimum window substring的start index, minLen表示该min window substring的长度,初始化为s.length() + 1.

然后我们移动end去找一个valid的window.首先看s.charAt(end)是不是在t里,这个时候我们检查hash里面有没有这个key并且要保证该key对应的value是大于0的.(可能遇到虽然t里有char a, 但是已经被找到了的情况,这个时候就不能继续像没找到时第一次找到那样操作). 如果有,那么我们就在找到了一个t里边有的char, 所以还需要找的char数目count就减1.
这句话蛮重要:hash.put(ch, hash.getOrDefault(ch, 0) - 1);
如果t里面有这个char,那现在找到了就得把它在t里的频数减1;如果t里面没有,说明hash以前也没有这个key, 我们就加一个这个key, 并且在它的默认value 0的基础上减一到-1.
为什么要这么写呢?因为后面while (count == 0)里面要用到. 就是当我们要移动start去找一个更小的valid windows时,会去检查s.charAt(start)是不是在t里,从而决定要不要对count增一。这时候我们先将频数还原:hash.put(cha, hash.getOrDefault(cha, 0) + 1);.这里如果原来的t里有该char,那得到的get(char)就肯定会大于0,如果原来没有,那么之前我们删频数的时候肯定得到get(char) == -1,所以现在的value会是0. 所以下一步我们只需要判断hash.get(char) > 0? 就可以决定要不要对count增一了。简单来讲就是我移动窗口左端点的时候,检查看看左端点那个char是不是t里面的,如果是我就得让需要在s里找到的在t里存在的char数目增加一,不是就不需要。

class Solution {
    public String minWindow(String s, String t) {
        Map hash = new HashMap<>();
        for (char c : t.toCharArray()){
            hash.put(c, hash.getOrDefault(c, 0) + 1);
        }
        //s = "ab"
        //t = "b"
        //hash:("a"->0, "b"->0)
        int start = 0, end = 0;
        int count = t.length();
        int minStart = 0, minLen = s.length() + 1;
        while (end < s.length()){
            char ch = s.charAt(end);
            if (hash.containsKey(ch) && hash.get(ch) > 0){
                count--; 
            }
            hash.put(ch, hash.getOrDefault(ch, 0) - 1);
            while (count == 0){
                if (end - start + 1< minLen){
                    minLen = end - start + 1;
                    minStart = start;
                }
                char cha = s.charAt(start);
                hash.put(cha, hash.getOrDefault(cha, 0) + 1);
                if (hash.containsKey(cha) && hash.get(cha) > 0){
                    count++;
                }
                start++;
            }
            end++; 
        }
        if (minLen == s.length() + 1){
            return "";
        } else {
            return s.substring(minStart, minStart + minLen);
        }
    }
}

对于hash的处理也可以一开始就把s的每一个char都当作hash的key,但value为0;只有在t里面存在的才统计频数。这样可能不那么confusing!

class Solution {
    public String minWindow(String s, String t) {
        Map hash = new HashMap<>();
        for (char c : s.toCharArray()){
            hash.put(c, 0);
        }
        for (char c : t.toCharArray()){
            hash.put(c, hash.getOrDefault(c, 0) + 1);
        }
        //s = "ab"
        //t = "b"
        //hash:("a"->0, "b"->0)
        int start = 0, end = 0;
        int count = t.length();
        int minStart = 0, minLen = s.length() + 1;
        while (end < s.length()){
            char ch = s.charAt(end);
            if (hash.get(ch) > 0){
                count--; 
            }
            hash.put(ch, hash.get(ch) - 1);
            while (count == 0){
                if (end - start + 1< minLen){
                    minLen = end - start + 1;
                    minStart = start;
                }
                char cha = s.charAt(start);
                hash.put(cha, hash.get(cha) + 1);
                if (hash.get(cha) > 0){
                    count++;
                }
                start++;
            }
            end++; 
        }
        if (minLen == s.length() + 1){
            return "";
        } else {
            
            return s.substring(minStart, minStart + minLen);
        }
    }
}

你可能感兴趣的:(76. Minimum Window Substring)