leetcode hot 100 76. 最小覆盖子串

76. 最小覆盖子串

已解答

困难

相关标签

相关企业

提示

给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 "" 。

注意:

  • 对于 t 中重复字符,我们寻找的子字符串中该字符数量必须不少于 t 中该字符数量。
  • 如果 s 中存在这样的子串,我们保证它是唯一的答案。

示例 1:

输入:s = "ADOBECODEBANC", t = "ABC"
输出:"BANC"
解释:最小覆盖子串 "BANC" 包含来自字符串 t 的 'A'、'B' 和 'C'。

示例 2:

输入:s = "a", t = "a"
输出:"a"
解释:整个字符串 s 是最小覆盖子串。

示例 3:

输入: s = "a", t = "aa"
输出: ""
解释: t 中两个字符 'a' 均应包含在 s 的子串中,
因此没有符合条件的子字符串,返回空字符串。

提示:

  • m == s.length
  • n == t.length
  • 1 <= m, n <= 105
  • s 和 t 由英文字母组成
class Solution(object):
    def minWindow(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: str
        """
        # 官方解决方法:
        need = collections.defaultdict(int)
        for c in t:
            need[c]+=1
        needCnt = len(t)
        i = 0
        res = (0,float('inf'))
        for j,c in enumerate(s):
            if need[c]>0:
                needCnt -=1 
            need[c] -= 1
            if needCnt == 0 :
                # 这里就是已经全部ok了尅开始右移作左边界了
                while True:
                    c=s[i]
                    if need[c] == 0:
                        break
                    need[c]+=1
                    i+=1
                if j - i  < res[1] - res[0]:
                    res = (i,j)
                need[s[i]] += 1
                needCnt += 1
                i+=1
        return '' if res[1]>len(s) else s[res[0]:res[1]+1]





        # 这个方法是自己写的方法:
        # left = 0
        # right = -1
        # # 两个函数right右移直到满足条件
        # # left右移直到不满足条件的前一个
        # hash_t={}
        # hash_s={}
        # for i in  t:
        #     hash_t[i] = hash_t.get(i,0)+1
        # count_t = len(hash_t.keys())
        # count_s = 0
        # res_str=''
        # res_len=111111111

        # def left_move(left,right,count_s,count_t,res_len,res_str):
        #     while count_s==count_t:
        #         if  hash_t.get(s[left],0):
        #             hash_s[s[left]] = hash_s.get(s[left],0)-1
        #             if  hash_s[s[left]] < hash_t[s[left]]:
        #                 count_s-=1
                        
        #                 if right - left+1<res_len:
        #                     res_len = right - left+1
        #                     res_str = s[left:right+1]

        #         left+=1
        #     # 判断一下是到头了还是满足了
        #     if left>=len(s):
        #             return -1,count_s,count_t,res_len,res_str
        #     else:
        #         return left,count_s,count_t,res_len,res_str


        # def right_move(right,count_s,count_t):
        #     while count_s<count_t:
        #         right+=1
        #         if right>=len(s):
        #             break

        #         if  hash_t.get(s[right],0):
        #             hash_s[s[right]] = hash_s.get(s[right],0)+1
        #             if  hash_s[s[right]] == hash_t[s[right]]:
        #                 count_s+=1
        #     # 判断一下是到头了还是满足了
        #     if right>=len(s):
        #             return -100,count_s,count_t
        #     else:
        #         return right,count_s,count_t

        # while right!=-100:
        #     right,count_s,count_t = right_move(right,count_s,count_t)
        #     # print( right,count_s,count_t)
        #     left,count_s,count_t,res_len,res_str = left_move(left,right,count_s,count_t,res_len,res_str)
        #     # print(left,count_s,count_t,res_len,res_str)

        # return res_str

        





这里我们可以使用第一种方法表示的是最好解法,这里的need对于t之中的东西,在满足之后会为0,但是对于不需要的东西如果在里面会是负数。按照这个特性我们写了代码。优化了原本的逻辑。

总的逻辑可以说是,right一直向右加,直到满足t,然后left加直到不满足t这个时候记录res,直到right到达边界停止。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值