sac sac - 1 month ago 10
C++ Question

Minimum no of characters to be deleted from both the strings to make it anagram

I have to find the minimum no of characters to be deleted in order to make the two strings anagram,but my code is not giving the exact value which is required:

My code:

int main() {

int arr[26]={0};
int i,cont=0;
char a[10000];
char b[10000];
cin>>a;
cin>>b;
int l1=strlen(a);
int l2=strlen(b);
for(i=0;i<l1;i++)
arr[a[i]-'a']+=1;
for(i=0;i<l2;i++)
arr[b[i]-'a']-=1;
for(i=0;i<26;i++)
{
if(arr[i]!=0)
{

cont=cont+abs(arr[i]);
}
}
cout<<cont;
return 0;
}


Explanation to my code:

I have created an array of
26
memory locations in it,initializing all to
0
.
Then I am generating the location
arr[i]
contains a character and if we subtract it by
'a'
,it will give the exact location:like if
arr[i]='a'
the
'a'-a'=0
,for the first string I am adding 1 to it and for the other string I am subtracting 1 to it as a result I am getting those locations or alphabets that are not matched ,I am counting them up in order to get the answer.

But for this testcase:

fcrxzwscanmligyxyvym // First string

jxwtrhvujlmrpdoqbisbwhmgpmeoke //Second string


The output :

30


Not getting the correct output

Answer

Your for loops scan first 26 characters of the input strings, despite the fact those strings may be shorter than or longer than 26 characters...

Comments