solution.cpp 599 字节
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
	string minWindow(string s, string t)
	{
		vector<int> count(128);
		for (char c : t)
		{
			count[c]++;
		}
		int l = 0, r = 0;
		int need_to_meet = t.length();
		int start, min_len = INT_MAX;
		while (r < s.length())
		{
			if (--count[s[r++]] >= 0)
			{
				need_to_meet--;
			}
			while (need_to_meet == 0)
			{
				if (r - l < min_len)
				{
					start = l;
					min_len = r - l;
				}
				if (++count[s[l++]] > 0)
				{
					need_to_meet++;
				}
			}
		}
		return min_len == INT_MAX ? "" : s.substr(start, min_len);
	}
};