Skip to content

Commit ee2d368

Browse files
Create 438-Find-All-Anagrams-In-A-String.cpp
1 parent 1972d20 commit ee2d368

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution{
2+
public:
3+
unordered_map<char, int> Create(string p){
4+
unordered_map<char, int> Mapp;
5+
for(char & i : p){
6+
if(Mapp.find(i) == Mapp.end()){
7+
Mapp.insert(make_pair(i, 1));
8+
}
9+
else{
10+
Mapp[i]++;
11+
}
12+
}
13+
return Mapp;
14+
}
15+
vector<int> findAnagrams(string s, string p){
16+
unordered_map<char, int> Maps, Mapp = {};
17+
vector<int> nums = {};
18+
int Fp, Sp;
19+
int lens, lenp;
20+
Fp = 0;
21+
Sp = p.length();
22+
lens = s.length();
23+
lenp = p.length();
24+
Mapp = Create(p);
25+
Maps = Create(s.substr(0, p.length()));
26+
for(Fp = 0; Fp < lens - lenp + 1; Fp++){
27+
if(Maps == Mapp){
28+
nums.push_back(Fp);
29+
}
30+
if(Maps.find(s[Sp]) != Maps.end()){
31+
Maps[s[Sp]]++;
32+
}
33+
else{
34+
Maps.insert(make_pair(s[Sp], 1));
35+
}
36+
Sp++;
37+
Maps[s[Fp]]--;
38+
if(Maps[s[Fp]] == 0){
39+
Maps.erase(s[Fp]);
40+
}
41+
}
42+
return nums;
43+
}
44+
};

0 commit comments

Comments
 (0)