Skip to content

Instantly share code, notes, and snippets.

@SuryaPratapK
Created May 17, 2020 14:37
Show Gist options
  • Save SuryaPratapK/93905c88c3e23477e4c3451d0db90a82 to your computer and use it in GitHub Desktop.
Save SuryaPratapK/93905c88c3e23477e4c3451d0db90a82 to your computer and use it in GitHub Desktop.
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
vector<int> ans;
vector<int> hash(26,0);
vector<int> phash(26,0);
int window = p.size();
int len = s.size();
if(len<window)
return ans;
int left = 0,right = 0;
while(right<window)
{
phash[p[right]-'a'] +=1;
hash[s[right++]-'a'] +=1;
}
right -=1;
while(right<len)
{
if(phash == hash)
ans.push_back(left);
right+=1;
if(right!=len)
hash[s[right]-'a'] +=1;
hash[s[left]-'a'] -=1;
left+=1;
}
return ans;
}
};
@SuryaPratapK
Copy link
Author

SuryaPratapK commented Sep 19, 2020 via email

@shashankrustagi
Copy link

best advice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment