76. Minimum Window Substring

题目来源:https://leetcode.com/problems/minimum-window-substring/

 自我感觉难度/真实难度:              写题时间时长:3hour

 题意:

 分析:

 自己的代码:

class Solution(object):
    def minWindow(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: str
        """
        # freq=collections.Counter(t)
        freq=collections.defaultdict(int)
        for ch in t:
            freq[ch]+=1
        miss=len(t)
        i=0
        start=end=0
        
        for j,char in enumerate(s,1):
            
            if freq[char] > 0:
                miss-=1
            freq[char]-=1
            if miss==0:
                while freq[s[i]]<0:
                    freq[s[i]]+=1
                    i+=1
                miss+=1
                freq[s[i]]+=1

                if end==0 or j-i < end-start:
                    end,start=j,i
                i+=1
        return s[start:end]

 

代码效率/结果:

 优秀代码:

class Solution(object):
    def minWindow(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: str
        """
        # freq=collections.Counter(t)
        freq=collections.defaultdict(int)
        for ch in t:
            freq[ch]+=1
        miss=len(t)
        i=0     #记录左边的位置
        start=end=0
        
        for j,char in enumerate(s,1):
             #j记录右边的位置
            if freq[char] > 0:
                miss-=1
            freq[char]-=1
            if miss==0:
                while freq[s[i]]<0:
                    freq[s[i]]+=1
                    i+=1
                miss+=1
                freq[s[i]]+=1

                if end==0 or j-i < end-start:
                    end,start=j,i
                i+=1
        return s[start:end]

 

  def minWindow(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: str
        """
        if not s or not t:
            return ""
        
        # Defaultdict is very useful in this problem, though i don't like to import modules
        target_count_dict = collections.defaultdict(int)
        for ch in t:
            target_count_dict[ch] += 1
        remain_missing = len(t)
        start_pos, end_pos = 0, float('inf')
        current_start = 0
        
        # Enumerate function makes current_end indexes from 1
        for current_end, ch in enumerate(s, 1):
            # Whenever we encounter a character, no matter ch in target or not, we minus 1 in count dictionary
            # But, only when ch is in target, we minus the length of remain_missing
            # When the remain_missing is 0, we find a potential solution.
            if target_count_dict[ch] > 0:
                remain_missing -= 1
            target_count_dict[ch] -= 1
            
            if remain_missing == 0:
                # Remove redundant character
                # Try to find the fist position in s that makes target_count_dict value equals 0
                # Which means we can't skip this character in s when returning answer
                while target_count_dict[s[current_start]] < 0:
                    target_count_dict[s[current_start]] += 1
                    current_start += 1
                if current_end - current_start < end_pos - start_pos:
                    start_pos, end_pos = current_start, current_end
                
                # We need to add 1 to current_start, and the correspondence value in dictionary, is because
                # this is the first character of the potential answer. So, in future iteration, when we encounter this character,
                # We can remove this currently first character to try to find a shorter answer.
                
          target_count_dict[s[current_start]] += 1 remain_missing += 1 current_start += 1 return s[start_pos:end_pos] if end_pos != float('inf') else ""

 

 

代码效率/结果:

 自己优化后的代码:

 反思改进策略:

1.对比较难的题目,学会从下面三个角度思考

2.这里使用的是双索引技术,注意思考如何更新左右边界的情况

 

posted @ 2019-05-09 21:46  dgi  阅读(131)  评论(0编辑  收藏  举报