我写了首诗,把滑动窗口算法变成了默写题 | labuladong 的算法小抄 (gitee.io)
windows放窗口里需要统计的元素
class Solution {
public:
bool checkInclusion(string s1, string s2) {
int left = 0;
int right = 0;
int flag = 0;
map<char, int> need;
for (int i = 0; i < s1.size(); i++)
{
need[s1[i]]++;
}
//cout<<need.size()<<endl;
map<char, int> windows;
char c;
while (right < s2.size())
{
c = s2[right];
right++;
if (need.count(c))
{
windows[c]++;
if (windows[c] == need[c])
{
flag++;
}
}
if (right - left == s1.size())
{
// cout<<right<<" "<<left<<endl;
// cout<<flag<<" "<<need.size()<<endl;
if (flag == need.size())
{
return true;
}
else
{
c = s2[left];
left++;
if (need.count(c))
{
if (windows[c] == need[c])
{
flag--;
}
windows[c]--;
}
}
}
}
return false;
}
};