Created
May 17, 2020 14:37
-
-
Save SuryaPratapK/93905c88c3e23477e4c3451d0db90a82 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
best advice.