Count Occurences of Anagrams | Practice | GeeksforGeeks

Given a word pat and a text txt. Return the count of the occurences of anagrams of the word in the text.
Example 1:
Input:
txt = forxxorfxdofr
pat = for
Output: 3
Explanation: for, orf and ofr appears
in the txt, hence answer is 3.

Example 2:
Input:
txt = aabaabaa
pat = aaba
Output: 4
Explanation: aaba is present 4 times
in txt.

Your Task:
Complete the function search() which takes two strings pat, txt, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(26) or O(256)
Constraints:
1 <= |pat| <= |txt| <= 105
Both string contains lowercase english letters.
 
0
sharmaadarsh859in 8 hours
//EASY SOLUTION USING SLIDING WINDOW AND MAPS
//PLEASE UPVOKE IF FOUND USEFUL
class Solution{public:int search(string pat, string txt) {    int i=0,j=0;    map<char,int>m;    map<char,int>n;    for(char a='a';a<='z';a++){        m[a]=0;        n[a]=0;    }    for(int i=0;i<pat.size();i++){        m[pat[i]]++;    }    int count=0;    while(j<txt.size()){        n[txt[j]]++;        if(j-i+1<pat.size())            j++;        else if(j-i+1==pat.size()){            if(m==n)                count++;            n[txt[i]]--;            i++;            j++;        }         }    return count;}
};
0
aryanguptta142 days ago
// EASY TO UNDERSTAND
// C++ SLIDING WINDOW 
 
 
class Solution{public:int search(string pat, string txt) {    // code here    int k=pat.size();    int res=0;    unordered_map<char,int> mp;    for(int i=0;i<k;i++) {        mp[pat[i]]++;    }        int count=mp.size();    int i=0,j=0;    while(j<txt.size()) {                mp[txt[j]]--;        if(mp[txt[j]]==0) count--;                     if((j-i+1)<k) j++;        else if((j-i+1)==k) {                   if(count==0) res++;            mp[txt[i]]++;             if(mp[txt[i]]==1) count++;            i++;j++;        }    }    return res;}
};
0
swetkrmis2 days ago
class Solution{public:int search(string pat, string txt) {    int n=txt.length();    int k=pat.length();    int i=0,ans=0;    int j=0;    unordered_map<char,int>mp;    unordered_map<char,int>mp1;    for(int i=0;i<k;i++){        mp[pat[i]]++;    }    for(int i=0;i<k;i++){        mp1[txt[i]]++;    }    i=0;    j=k-1;    int count=0;    while(j<n){        int count1=0;        for(auto i:mp){            if(mp1[i.first]!=i.second) break;            count1++;        }        if(count1==mp.size()) count++;        mp1[txt[i]]--;        i++;        j++;        mp1[txt[j]]++;    }    return count;}
};
+2
ekansh_g223 days ago
//EASY TO UNDERSTAND CODE
 
 int search(string pat, string txt) {   int n = txt.length();   int k = pat.length();   int i = 0,ans=0;   int j =0;   unordered_map<char,int>mp;   for(int i =0;i<k;i++)   {       mp[pat[i]]++;   }   int count=mp.size();   while(j<n){       if(mp.find(txt[j])!=mp.end())       {           mp[txt[j]]--;           if(mp[txt[j]]==0)           {               count--;           }       }       if(j-i+1<k)       {           j++;       }       else if(j-i+1==k){           if(count==0)           {               ans++;           }              if(mp.find(txt[i])!=mp.end())       {           mp[txt[i]]++;           if(mp[txt[i]]==1)           {               count++;           }       }       i++;       j++;       }   }   return ans;}
0
dibyenducse214 days ago
int search(string pat, string txt) {    int count=0;    vector<int>vtaxt(26, 0);    vector<int>ptaxt(26, 0);        int len=txt.size();    int y=pat.size();    for(int i=0; i<y; i++){        vtaxt[txt[i]-'a']++;        ptaxt[pat[i]-'a']++;    }   if(vtaxt==ptaxt)  count++;    for(int j=1; j<(len-y+1); j++) {        vtaxt[txt[j+y-1]-'a']+=1;        vtaxt[txt[j-1]-'a']-=1;        if(vtaxt==ptaxt) count++;    }    return count;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.