LeetCode/solutions/438. Find All Anagrams in a String.md

78 lines
3.3 KiB
Markdown
Raw Permalink Normal View History

# [438. Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/description/)
# 思路
## 思路一
用固定长度(p.size)的滑动窗口在s上滑动每次判断是否满足题意即可。
用一个数组p_count记录每个字符出现的次数对p中出现的字符进行累加对窗口中出现的字符进行累减若这个数组的元素全为0则满足题意。
时间复杂度O(n), 空间复杂度O(1)
每次判断p_count是否全为0时最坏都要遍历整个数组比较费时间。思路二将解决这个问题
## 思路二*
和思路一类似先用一个数组p_count记录p中各字符出现的次数。然后初始化一个长度为0的窗口low = high = 0。
第一步先扩展窗口也就是在右边界high上做文章。每次high读到s的一个字符char当p_count[char]的值大于0时很明显就是窗口中进入了一个p中含有的字符。
我们可以取一个变量char_num值初始为p中所有字符的总数。每次有一个p中字符从high进入窗口就char_num-, 每次有p的字符从窗口low出去就char_num++。
这样当char_num == 0的时候表明我们的窗口中包含了p中的全部字符得到一个结果。
# C++
## 思路一
2019-09-13 15:08:41 +00:00
```C++
class Solution {
private:
bool isOK(vector<int>count){
for(int num: count)
if(num != 0) return false;
return true;
}
public:
vector<int> findAnagrams(string s, string p) {
vector<int>p_count(26, 0), res;
if(p.size() > s.size()) return res;
for(int i = 0; i < p.size(); i++){ // 处理第一个窗口
p_count[p[i] - 'a']++;
p_count[s[i] - 'a']--;
}
int pos = 0;
while(1){ // 窗口不断移动
if(isOK(p_count)) res.push_back(pos);
pos++;
if(pos + p.size() > s.size()) break;
p_count[s[pos - 1] - 'a']++;
p_count[s[pos + p.size() - 1] - 'a']--;
}
return res;
}
};
```
## 思路二
2019-09-13 15:08:41 +00:00
```C++
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
vector<int>p_count(26, 0), res;
int char_num = p.size(); // char_num代表在p在窗口中的字符数
if(char_num > s.size()) return res;
for(int i = 0; i < p.size(); i++) p_count[p[i] - 'a']++; // p_count记录p中出现的字母次数
int low = 0, high = 0;
// 这个for循环完全可以不加因为后面的while循环会完成同样的工作不过加上后由于少执行了一个if语句所以会快一些
for(; high < p.size() - 1; high++){ // 窗口初始化成p.size-1大小(因为while里面会立马增大窗口大小)
if(p_count[s[high] - 'a'] > 0) char_num--;
p_count[s[high] - 'a']--;
}
while(high < s.size()){
if(p_count[s[high] - 'a'] > 0) char_num--; // 窗口右界右移
p_count[s[high] - 'a']--;
high++;
if(char_num == 0) res.push_back(low); // char_num 等于0 代表p中字符全在窗口中
if(high - low == p.size()){ // 窗口超过长度限制,左界右移
if(p_count[s[low] - 'a'] >= 0) char_num++;
p_count[s[low] - 'a']++;
low++;
}
}
return res;
}
};
```