leetcode 随笔 Minimum Window Substring --双指针&hash

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.

问题很好理解,就不再描述了。

面对这种A包含B所有字符的问题要么是先对B排序要么就是利用hash,这里很明显没办法利用排序,题目中也给了要求要求复杂度在O(n),所以这里我们只能利用哈希。

这个题我在没有看discuss之前得到的答案的runtime是26ms,求解办法是先求出初始的“窗口”,然后再移动窗口,如果在移动中窗口包含了T,那么窗口从窗口开始的地方开始缩小。hash的方法是利用unordered_map<char,int>

class Solution {
public:
string minWindow(string s, string t) {
		if (s.empty() || t.empty()) return "";
		unordered_map<char,int> record;
		int sindex = 0, eindex = s.size() - 1;
		for (int i = 0; i < t.size(); i++)
		{
			if (record.count(t[i]) == 0)
				record.insert(std::make_pair(t[i], 1));
			else
				record[t[i]]++;
		}
		int tt = record.size(), i = sindex;
		while (record.count(s[sindex]) == 0) sindex++;
		for (; i <= eindex; i++)
		{
			if (record.count(s[i]))
			{
				if (--record[s[i]] == 0)
				{
					tt--;
				}
			}
			if (tt == 0)
				break;
		}
		if (tt) return "";
		while (sindex < i)
		{
			if (record.count(s[sindex]))
			{
				if (++record[s[sindex]] == 1)
				{
					record[s[sindex]]--;
					break;
				}
			}
			sindex++;
		}
		int rs = sindex, re = i, ns = sindex, ne = i+1;
		while(ne<=eindex)
		{
			if (record.count(s[ns]))
			{
				if (++record[s[ns]] == 1)
				{
					tt++;
				}
			}
			if (record.count(s[ne]))
			{
				if (--record[s[ne]] == 0)
				{
					tt--;
				}
			}
			if (tt == 0)
			{
				while (ns < ne)
				{
					ns++;
					if (record.count(s[ns]))
					{
						if (++record[s[ns]] == 1)
						{
							record[s[ns]]--;
							ns--;
							break;
						}
					}

				}
				rs = ns+1;
				re = ne;
			}
			ne++;
			ns++;
		}
		return s.substr(rs, re - rs+1);
	}                                                                                                                            
};

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

看完discuss的感觉就是整体的思路其实是差不多的,但是明显别人的代码就显得更加巧妙一些。

利用vector<int>(128,0) 进行map存储,vector与字符的对应关系即为:i -->vector[i] 利用的是字符转换为int的值

public:
    string minWindow(string s, string t) {
        if(s.empty()) {
            return "";
        }
        
        vector<int> map(128, 0);
        int count = 0;
        for (auto & c : t) {
            map[c]++;
            count++;
        }
        
        int start = 0, end = 0, head = 0, d = INT_MAX;
        
        while(end < s.size()) {
            if (map[s[end++]]-- > 0) {
                count--;
            }
            
            while (count == 0) {
                if (end - start < d) {
                    d = end - start;
                    head = start;
                }
                if(map[s[start++]]++ >= 0) {
                    count++;
                }
            }
            
        }
        
        return d != INT_MAX ? s.substr(head, d) : "";
    }
};

答案是一旦得到一个合法窗口(count==0时),就无脑把窗口的大小-1。

答案的runtime是13ms,明显要更快。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值