Efficient and O(n) code for this in c??
I know that solution of O(n*n)
stringCompare(str1, str2){
int freq1[100] = {0}, i;
int freq2[100] = {0};
for(i=0; i<=strlen(str1); i++){
freq1[str1[i]]+ = 1;
}
for(i=0; i<=strlen(str2); i++)
{
freq2[str2[i]]+ = 1;
}
for(i=0;i<26;i++){
if(freq1[i]!=freq2[i])
return 0;
return 1;
}
}
I modified
MAK‘s pseudocode slightly so it only uses one frequency count array. A positive value in the final freq array means a char ins1is not ins2. A negative value signals extra chars ins2.