[HashTable/SlideWindow]076 Minimum Window Substring***

  • 分类:HashTable/SlideWindow

  • 考察知识点:HashTable SlideWindow

  • 最优解时间复杂度:**O(n) **

76. Minimum Window Substring

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.

代码:

我本来的解法:

class Solution:
    def minWindow(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: str
        """
        subs=""
        #判断临界条件
        if len(s)==0 or len(t)==0 or len(t)>len(s):
            return subs
        total_count=0
        total=len(t)
        count_dict={}
        list_dict={}
        for ch in t:
            if ch not in count_dict:
                count_dict[ch]=1
                list_dict[ch]=[]
            else:
                count_dict[ch]+=1
        order=[]
        minlen=len(s)

        for i,ch in enumerate(s):
            if ch in count_dict:
                if count_dict[ch]>0:
                    total_count+=1
                count_dict[ch]-=1
                order.append(ch)
                list_dict[ch].append(i)
                if len(order)>=total:
                    while count_dict[order[0]]<0:
                        count_dict[order[0]]+=1
                        list_dict[order[0]].pop(0)
                        order.pop(0)

                    if total_count==total and list_dict[order[-1]][-1]-list_dict[order[0]][0]+1<=minlen:
                        minlen=list_dict[order[-1]][-1]-list_dict[order[0]][0]+1
                        subs=s[list_dict[order[0]][0]:list_dict[order[-1]][-1]+1]

        return subs

最佳解法:

class Solution:
    def minWindow(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: str
        """
        subs=""
        #判断临界条件
        if len(s)==0 or len(t)==0 or len(t)>len(s):
            return subs

        total=len(t)
        start,end=0,0
        count_dict={}
        for ch in t:
            if ch not in count_dict:
                count_dict[ch]=1
            else:
                count_dict[ch]+=1

        j=0
        for i,ch in enumerate(s,1):
            if ch in count_dict:
                if count_dict[ch]>0:
                    total-=1
                count_dict[ch]-=1

                if total==0:
                    while (j

讨论:

1.这个题是一道特别难的题,也是一道特别重要的题。
2.虽然leetcode里面没有分类,但是这道题考察的是SlideWindow,是一个面试中经常考到的知识点,非常重要
3.我本来的想法是搞两个dict和一个list,一个dict是t的一个count_dict,第二个dict是一个t中数据每个的位置,一个list放的是在出现的order。当len(order)>len(t)的时候,看看这些数是不是全在里面,如果全在里面的话,看第一个数是不是多余的。这个方法感觉还是太费空间了,貌似run出来也很慢?
4.最佳的这个SlideWindow方法,是有个total,当total等于0的时候就是所有的数都出现的时候,再寻找它开始的位置,记下来,然后去掉那个开始的位置,再继续寻找。

SlideWindow

你可能感兴趣的:([HashTable/SlideWindow]076 Minimum Window Substring***)