76 - Minimum Window Substring

76 - Minimum Window Substring

Follow up: return index, instead of substring 


https://www.youtube.com/watch?v=9qFR2WQGqkU

Sliding window 模版

while(fast < n){ // for each fast (iterating from left to right)
  1. add fast computstion into the sliding window
    // the sliding window includes the fast
    //[slow, fast] as the sliding window
    
  2. move slow to the dedesired position 
    while(slow is in the desired position){
      slow++;
    }
}





===============

class Solution{
  public String minWindow(String s, String t) {
    if(s.length() < t.length()){
      return "";
    }
    
    Map<Character, Integer> map = constructWordDict(t);
    int slow = 0, minLen = Integer.MAX_VALUE, matchCount = 0, index = 0;
    for(int fast = 0; fast < s.length(); fast++){
      char ch = s.charAt(fast);
      Integer count = map.get(ch);
      if(count == null){
        continue;
      }else{
        map.put(ch, count - 1);
        if(count == 1){
          matchCount++;
        }
        while(matchCount == map.size()){
          // find a valid substring 
          if(fast - slow + 1 < minLen){
            minLen = fast - slow + 1;
            index = slow;
          }
          char leftMost = s.charAt(slow++);
          Integer leftMostCount = map.get(leftMost);
          if(leftMostCount == null){
            continue;
          }else{
            map.put(leftMost, leftMostCount + 1);
            if(leftMostCount == 0){
              matchCount--;
            }
          }
        }
      }
    }
    return minLen == Integer.MAX_VALUE ? "" : s.substring(index, index + minLen);
  }
  
  private Map<Character, Integer> constructWordDict(String t){
    Map<Character, Integer> map = new HashMap<>();
    for(char ch : t.toCharArray()){
      Integer count = map.get(ch);
      if(count == null){
        map.put(ch, 1);
      }else{
        map.put(ch, count + 1);
      }
    }
    return map;
  }
}


// initialize slow, fast pointer to move around
// initalize a hashmap to record the matching status
// the map(key: distinct char in the t, value: the freq of the char appears in the t)
// in order to make the check matching status easier 
// we use a match count to represent if the current substring is 
// a matching string or not. 
// if one freq becomes 0 from 1, we do count++,
// if one freq becomes 1 from 0, count--;
// if one freq becomes -1 to 0 or from 0 to -1, we dont care, because it still satisflies to be 
// a matching string 
// 
// first , when the match count is 0, not the size of the map, we move the fast pointer
// until we have a matching string and the match count should be the size of the map now
// now the match count is size of the map
// now. we want to shorten the matching string by moving the slow pointer to the right 
// two things could happen,
//
// (1). if we the elememt we are leaving behind right now is a char at the 
// map, we need to update the map by freq++, the freq would become 1 from 0, so we also do count--;
// now the current substring is no longer a matcing string, so we need to move the fast pointer to the right 
//
// (2). if the element we are leaving behind is not a char from the map, then we keep moving the 
// left pointer to the right, and since the matching count is still the size of the map and 
// the matching string is getting shorter, we need to update the length of the shortest matching
// string and the index, the index should be the slow pointer index. 

// finallly , when the slow pointer is at the end , and the fast pointer cant move anymore 
// we have an index which is the starting index of the shortest matching string, and the 
// shortest matching string len is the length, so the result should be substring (index, index + length)
// if we find a matching string, otherwise, we are left with out initilized length, which is interger.MAX_VALUE

// Input: S = "ADOBECODEBANC", T = "ABC"
// Output: "BANC"

 

76. Minimum Window Substring
This code is more concise than LaiOffer’s 

Time o(n), n is the length if s 
Space o(t), t is the length of t 


class Solution {
    public String minWindow(String s, String t) {
      HashMap<Character, Integer> map = new HashMap<>();
      
      // build the freq map for t 
      for(Character c : t.toCharArray()){
        if(!map.containsKey(c)){
          map.put(c, 1);
        }else{
          map.put(c, map.get(c) + 1);
        }
      }
      
      int number = 0;
      int uniqueChar = map.size();
      
      int i = 0;
      int j = 0;
      int min = Integer.MAX_VALUE;
      int start = 0;
      
      while(j < s.length()){
        
        // move j 
        if(number < uniqueChar){
          char c = s.charAt(j++); // 在这里进行 j++ , 如果在最后面进行的话, 
// 如果这个map 不包含 c, continue, 就没法 j++ 了。 同理 下面 i++的时候
          if(!map.containsKey(c)){
            continue;
          }else{
            // its in the map
            int freq = map.get(c);
            if(freq == 1){
              number++;
            }
            map.put(c, freq - 1);
          }
        }
        
        
        // number = uniqueChar, move i 
        while(number == uniqueChar){
          if(min > j - i ){
            min = j - i;
            start = i;
          }
          
          
          
          char c = s.charAt(i++);
          if(map.containsKey(c)){
            
            int freq = map.get(c);
            if(freq == 0){
              number--;
            }
            map.put(c, freq + 1);
          }else{
            continue;
          }
        }
        
      }
      
      if(min == Integer.MAX_VALUE){
        return "";
      }else{
        return s.substring(start, start + min);
          
      }
    }
}

https://leetcode.com/problems/minimum-window-substring/discuss/26808/Here-is-a-10-line-template-that-can-solve-most-'substring'-problems
这个模版注意一下, while end < length
                                          If ()
                        j++while()
                        I++;

 

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).

Example:

Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"

Note:

  • If there is no such window in S that covers all characters in T, return the empty string "".
  • If there is such window, you are guaranteed that there will always be only one unique minimum window in S.

posted on 2018-08-09 17:28  猪猪&#128055;  阅读(150)  评论(0)    收藏  举报

导航