question: Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] and its permutations (or anagrams) in txt[]. You may assume that n > m.
#include<iostream>
#include<cstring>
#define MAX 256
using namespace std;
void search(char *pat, char *txt)
{
int M = strlen(pat), N = strlen(txt);
int i,count=0,start=0 ;
int hashpat[26]={0},hashtxt[26]={0};
for(i=0;i<M;i++)
{
hashpat[pat[i]]++;
}
for(i=0;i<N;i++)
{
hashtxt[txt[i]]++;
if(hashtxt[txt[i]]<=hashpat[txt[i]])
count++;
if(count==M)
{ cout<<"Found at index"<<i-M<<"\n";
hashtxt[txt[start]]--;
if(hashpat[txt[start]]!=0) count--;
start++;
}
}
}
/* Driver program to test above function */
int main()
{
char txt[] = "BACDGABCDA";
char pat[] = "ABCD";
search(pat, txt);
return 0;
}