Problem Statement:
Given a word and a text, return the count of the occurences of anagrams of the word in the text. For eg. word is “for” and the text is “forxxorfxdofr”, anagrams of “for” will be “ofr”, “orf”,”fro”, etc. So the answer would be 3 for this particular example.
The above is an interview question I saw here.
Here is the code I have written:
#include <iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int equals(char* A, char* B)
{
int i,len = strlen(A);
int a[26],b[26];
for(i=0;i<26;i++)
{
a[i]=0;
b[i]=0;
}
for(i=0;i<len;i++)
{
a[A[i]-'a']++;
}
for(i=0;i<len;i++)
{
b[B[i]-'a']++;
}
for(i=0;i<26;i++)
{
if(a[i]!=b[i])
return 0;
}
return 1;
}
int check(char* s1, char* s2)
{
int i,j,len1=strlen(s1), len2=strlen(s2), flag=0;
char arr[len2];
for(i=0;i<=len1-len2;i++)
{
for(j=0;j<len2;j++)
{
arr[j]=s1[i+j];
}
if(equals(arr,s2))
flag++;
}
return flag;
}
int main()
{
char text[50]="",pat[50]="";
cout<<"Enter original string: ";
cin>>text;
cout<<"Enter pattern string: ";
cin>>pat;
cout<<"No. of anagrams in text are :"<<check(text,pat);
return 0;
}
(I have also mentioned this code in my technical blog).
Questions:
- How can I improve my code in terms of its complexity and efficiency? As of now, it uses an approach similar to the naive string matching algorithm.
- Is my method to not compute all permutations and instead use an array good enough, or are there any better approaches?