LeetCode —— Minimum Window Substring

本文详细解析了LeetCode第76题“最小覆盖子串”的解决方案,介绍了使用滑动窗口与哈希表实现复杂度为O(n)的高效算法。

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

链接:http://leetcode.com/onlinejudge#question_76

原题:

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).

For example,
S = "ADOBECODEBANC"
T = "ABC"

Minimum window is "BANC".

Note:
If there is no such window in S that covers all characters in T, return the emtpy string "".

If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

思路:这题想了好长时间,STL的unordered_map也用的不熟,调试也很痛苦。

主要思想是用hash表存储T子串,用首尾索引来跟踪窗口。

第一步:找到一个包含T字符的窗口

第二步:把头部不必要的字符去掉,得到一个窗口长度值

第三步:继续把尾部索引向前走,如果出现和首部索引一样的字母,那么去掉头部的一些不必要字母,

形成新的窗口,比较长度值。

代码写丑了,写完后,查了一下,感觉自己的代码组织能力实在是太差了,这位大神给了个很漂亮的代码

http://blog.youkuaiyun.com/tingmei/article/details/8049577

class Solution {
public:
    string minWindow(string S, string T) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (S.size() < T.size() || T.size() == 0)
            return "";
        
        unordered_multiset<char> hashSet;
    	unordered_map<char, int> counter;
		unordered_map<char, int> benchmark;
		for (int i=0; i<T.size(); i++) {
			if (benchmark.find(T[i]) != benchmark.end())
				benchmark[T[i]] += 1;
			else
				benchmark[T[i]] = 1;
		}
        for (int i=0; i<T.size(); i++)
            hashSet.insert(T[i]);
        int start;
        int n = 0;
        vector<bool> flags(S.size(), false);
        for ( ; n < S.size(); n++) {
			unordered_multiset<char>::iterator itr = hashSet.find(S[n]);
            if (itr != hashSet.end()) {
                hashSet.erase(itr);
                start = n;
                flags[n] = true;
				counter[S[n]] = 1;
                break;
            }
        }
        if (hashSet.empty())
            return S.substr(start, 1);
        for (n++; n < S.size(); n++) {
            unordered_multiset<char>::iterator itr = hashSet.find(S[n]);
            if (itr != hashSet.end())
                hashSet.erase(itr);
			if (benchmark.count(S[n]) > 0) {
				flags[n] = true;
				if (counter.count(S[n]) > 0)
					counter[S[n]] += 1;
				else
					counter[S[n]] = 1;
			}
            
            if (hashSet.empty())
                break;
        }
        
        if (!hashSet.empty())
            return "";
        int pos = start;
		while (true) {
			if (flags[pos]) {
				if (benchmark[S[pos]] < counter[S[pos]])
					counter[S[pos]]--;
				else
					break;
			}
			pos++;
		}
		start = pos;
		int minLen = n - start + 1;
		
        for (n++; n < S.size(); n++) {
            if (counter.find(S[n]) != counter.end()) {
				counter[S[n]] += 1;
                flags[n] = true;
			}
            if (S[n] == S[start] ){
				while (true) {
					if (flags[start]) {
						if (benchmark[S[start]] < counter[S[start]])
							counter[S[start]]--;
						else
							break;
					}
					start++;
				}
                if (n - start + 1 < minLen) {
                    minLen = n - start + 1;
                    pos = start;
                }
            }
        }
        
        return S.substr(pos, minLen);
    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值