problem:https://leetcode.com/problems/minimum-window-substring/
滑动窗口题。维护一个包含t中所有字符的最小滑动窗口,首先用一个hashmap记录所有t中的字符和出现次数,在s中每遇到一次计数器加一,找到了符合条件的窗口后,尝试向右移动窗口左指针,直到恰好能够满足条件为止。更新当前最小滑动窗口。
class Solution {
public:
string minWindow(string s, string t)
{
vector<int> dstChar(256,0);
for(auto c: t)
{
dstChar[c]++;
}
string res;
int count = t.size();
int begin = 0;
for(int i = 0;i < s.size(); i++) // end
{
dstChar[s[i]]--;
if(dstChar[s[i]] >= 0)
{
count--;
}
if(count == 0)
{
while(dstChar[s[begin]] + 1 <= 0)
{
dstChar[s[begin]]++;
begin++;
}
int len = i - begin + 1;
if(res.empty() || len < res.size())
{
res = s.substr(begin, len);
}
}
}
return res;
}
};