-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path438.找到字符串中所有字母异位词.cpp
48 lines (45 loc) · 1.37 KB
/
438.找到字符串中所有字母异位词.cpp
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
38
39
40
41
42
43
44
45
46
47
/*
* @lc app=leetcode.cn id=438 lang=cpp
*
* [438] 找到字符串中所有字母异位词
*/
// @lc code=start
class Solution {
public:
vector<int> findAnagrams(string s, string t) {
// c++ 是有 unordered_map 的
unordered_map<char, int> need, window;
for (char c: t) need[c]++;
int left = 0, right = 0;
int valid = 0;
vector<int> res; // 记录结果
while (right < s.size()) {
char c = s[right];
right++;
// 进行窗口内数据的一系列更新
if (need.count(c)) {
window[c]++;
if (window[c] == need[c])
valid++;
}
// 判断左侧窗口是否要收缩
// while 判断条件为收缩条件
while (right - left >= t.size()) {
// 当窗口符合条件时,把起始索引加入 res
if (valid == need.size())
res.push_back(left);
// 左边被收缩的字符
char d = s[left];
left++;
// 进行窗口内数据的一系列更新
if (need.count(d)) {
if (window[d] == need[d])
valid--;
window[d]--;
}
}
}
return res;
}
};
// @lc code=end