Make it Anagram : HackerRank Problem Solution


Given two strings, count and print the number of characters which is to be deleted to make the two strings anagrams.

Input:

cde

abc

Output:


4


Explanation:


We need to delete 4 characters to make both strings anagram. i.e., 'd' and 'e' from first string and 'b' & 'a' from second string.


Implementation:


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
    char *s,*v;
    int hash[26];
    int i;
    int sum=0;
    memset(hash,0,sizeof(hash));
    s=(char*)malloc(sizeof(char)*10000);
    v=(char*)malloc(sizeof(char)*10000);
    scanf("%s",s);
    scanf("%s",v);
    i=0;
    while(*(s+i))
        {
        hash[*(s+i)-'a']++;
        i++;
    }
    i=0;
    while(*(v+i))
        {
        hash[*(v+i)-'a']--;
        i++;
    }
    for(i=0;i<26;i++)
        {
        if(hash[i]>0)
            sum+=hash[i];
        else if(hash[i]<0)
            sum+=abs(hash[i]);
    }
    printf("%d",sum);
    return 0;
}