I have a function code for string comparison as below :
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int max=0;
int calcMis(char *string,int i, int j,int len)
{
int mis=0;
int k=0;
while(k<len)
{
if(string[i+k]!=string[j+k])
mis+=1;
if((mis+len-k-1)<=max)
return 1;
else if(mis>max)
return 0;
k=k+1;
}
}
int main()
{
char *input=malloc(2000*sizeof(char));
scanf("%d",&max);
scanf("%s",input);
int c=0,i,j,k,x,mis=0;
int len=strlen(input);
i=0;
while(i<len-1)
{
j=i;
while(j<len-1)
{
k=i+1;
x=j-i+1;
if(x<=max)
c=c+len-k-x+1;
else
while(k+x<=len)
{
if(strncmp(input+i,input+k,x+1)==0)
{
if(max>=0)
c=c+x;
}
else
c+=calcMis(input,i,k,x);
k=k+1;
}
j=j+1;
}
i=i+1;
}
printf("%d",c);
return 0;
}
This codes is the solution for the question :
Given a string S and and integer K, find the integer C which equals the number of pairs of substrings(S1,S2) such that S1 and S2 have equal length and Mismatch(S1, S2) <= K where the mismatch function is defined below.
eg : abc then the substrings are {a,ab,abc,b,bc,c}
Is there any better method than this. Is there any optimizations possible in this code?