File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments