76. Minimum Window Substring

博客围绕LeetCode问题展开,要求时间复杂度为O(n)。通过定义两个指针i、j,i从左向右移动找所有T后停止,再移动j。还定义变量counter统计剩余需找的T中元素个数,数组count统计各元素需被找到的个数,最后给出AC代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 思路

 要求时间复杂度为O(n),可以定义两个指针i,j,指针i从左向右移动,当找到所有的T时i停止,开始移动指针j,当j的下一次移动使得s[j:i+1]没有包含T时停止,最后判断找到的i+1-j长度是不是最小的,即找到所谓的最小窗口。想法很简单,关键是找到一个数据结构表达T有没有“找全”,并且判断是否找到T的元素。

可以定义一个变量counter统计剩余需要找到的T中元素的个数,同时还要判断是否找到一个有效T中的元素,也就是说T中的元素找多了也没用。定义另一个数组count统计每个元素需要被找到的个数(可能为负,负数表示多找多了)。i移动时count元素值作减一操作,表示已经找到了;j移动时作加一操作,表示i找到的让j给丢掉了,请i再找一次。

2. AC代码

class Solution(object):
    def minWindow(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: str
        """
        count = [0 for _ in range(128)]
        for i in t:
            count[ord(i)] += 1
        counter = len(t)
        res_len = float('inf')
        res = ''
        j = 0
        for i in range(len(s)):
            if count[ord(s[i])] > 0:
                counter -= 1
            count[ord(s[i])] -= 1
            while(counter==0):
                if i - j + 1 < res_len:
                    res_len = i - j + 1
                    res = s[j:i+1]
                count[ord(s[j])] += 1
                if count[ord(s[j])] > 0:
                    counter += 1
                j += 1
        return res
                

 

To solve this problem, we can use the sliding window approach again. Here's the algorithm: 1. Initialize two dictionaries: need and window. need stores the count of each character in t, and window stores the count of each character in the current window. 2. Initialize two pointers left and right to mark the current window, and two variables match and required to track the number of matched characters and the number of required characters respectively. 3. Initialize a variable min_len to a large value and a variable start to 0 to store the start index of the minimum window substring. 4. While the right pointer is less than the length of the string s: - If the character at s[right] is in need, add it to window and update match and required accordingly. - While all characters in need are included in window, update min_len and start accordingly, and remove the character at s[left] from window and update match and required accordingly. - Move the left pointer to the right. - Move the right pointer to the right. 5. Return the minimum window substring starting from index start and having length min_len, or the empty string if no such substring exists. Here's the Python code for the algorithm: ``` def min_window(s, t): need = {} for c in t: need[c] = need.get(c, 0) + 1 window = {} left = right = 0 match = 0 required = len(need) min_len = float('inf') start = 0 while right < len(s): if s[right] in need: window[s[right]] = window.get(s[right], 0) + 1 if window[s[right]] == need[s[right]]: match += 1 while match == required: if right - left + 1 < min_len: min_len = right - left + 1 start = left if s[left] in need: window[s[left]] -= 1 if window[s[left]] < need[s[left]]: match -= 1 left += 1 right += 1 return s[start:start+min_len] if min_len != float('inf') else "" ``` Example usage: ``` s = "ADOBECODEBANC" t = "ABC" print(min_window(s, t)) # Output: "BANC" ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值