Skip to content

Commit

Permalink
Solution to #17
Browse files Browse the repository at this point in the history
  • Loading branch information
bharatr21 committed May 18, 2020
1 parent 6363910 commit e525db9
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Solutions/S17.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
vector<int> findAnagrams(string s, string p) {
vector<int> res;
int fre[26] = {0};
if(s.size() < p.size()) return res;
for(char c: p) {fre[c-'a']++;}
int st = 0, end = 0, cnt = p.size();
while(end < s.size())
{
//Two pointer approach
// If the starting character appeared in p, increment and move start pointer
if(end - st == p.size() && fre[s[st++] - 'a']++ >= 0) cnt++;

//If ending character appeared in p {since it's not < 0}, decrement and move end pointer
if(--fre[s[end++]-'a'] >= 0) cnt--;

//We have found one occurrence/match, since entire pattern size exhausted
if(cnt == 0) res.push_back(st);
}
return res;
}

0 comments on commit e525db9

Please sign in to comment.