vector<int> findAnagrams(string s, string p) {
unordered_map<char, int> need, window;
for (auto it:p) need[it]++;
int l = 0, r = 0;
bool flag1 = false;
vector<int> v;
while (r <= s.size()) {
if (!flag1)
window[s[r++]]++;
bool flag2 = false;
for (auto it:need)
if (window[it.first] < it.second) {
flag2 = true;
break;
}
if (flag2)
//当前还没有满足条件
flag1 = false;
else {
flag1 = true;
if (r - l == p.size()) v.emplace_back(l);
window[s[l++]]--;
}
}
return v;
}
438. 找到字符串中所有字母异位词
最新推荐文章于 2022-07-25 20:35:25 发布