ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

76. Minimum Window Substring

2019-05-09 21:50:51  阅读:237  来源: 互联网

标签:current ch end target start Substring 76 Window freq


题目来源: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.这里使用的是双索引技术,注意思考如何更新左右边界的情况

 

标签:current,ch,end,target,start,Substring,76,Window,freq
来源: https://www.cnblogs.com/captain-dl/p/10841293.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有