-
Notifications
You must be signed in to change notification settings - Fork 12
/
438.cpp
37 lines (36 loc) · 808 Bytes
/
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
class Solution {
public:
bool isAnagram(int fs[],int fp[]){
for(int i=0;i<26;i++){
if(fs[i]!=fp[i])return false;
}
return true;
}
vector<int> findAnagrams(string s, string p) {
int fp[26]={0},fs[26]={0};
vector<int>result;
int n=s.size(),m=p.size();
for(int i=0;i<m;i++){
fp[p[i]-'a']++;
}
for(int i=0;i<n;i++){
if(i<m-1){
fs[s[i]-'a']++;
}
else if(i==m-1){
fs[s[i]-'a']++;
if(isAnagram(fs,fp)){
result.push_back(i-m+1);
}
}
else {
fs[s[i-m]-'a']--;
fs[s[i]-'a']++;
if(isAnagram(fs,fp)){
result.push_back(i-m+1);
}
}
}
return result;
}
};