Alice is taking a cryptography class and finding anagrams to be very useful. We consider two strings to be anagrams of each other if the first string's letters can be rearranged to form the second string. In other words, both strings must contain the same exact letters in the same exact frequency For example,
bacdc
and dcbac
are anagrams, but bacdc
and dcbad
are not.Alice decides on an encryption scheme involving two large strings where encryption is dependent on the minimum number of character deletions required to make the two strings anagrams. Can you help her find this number?
Given two strings, and , that may or may not be of the same length, determine the minimum number of character deletions required to make and anagrams. Any characters can be deleted from either of the strings.
This challenge is also available in the following translations:
Input Format
The first line contains a single string, .
The second line contains a single string, .
The second line contains a single string, .
Constraints
- It is guaranteed that and consist of lowercase English letters.
Output Format
Print a single integer denoting the number of characters which must be deleted to make the two strings anagrams of each other.
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int makingAnagrams(char* s1, char* s2){
// Complete this function
int count1[26],count2[26];
for (int arr_i=0;arr_i<26;arr_i++)
{
count1[arr_i]=count2[arr_i]=0;
}
for(int arr_i=0;arr_i<strlen(s1);arr_i++)
{
count1[(s1[arr_i])-97]++;
}
for(int arr_i;arr_i<strlen(s2);arr_i++)
{
count2[(s2[arr_i])-97]++;
}
int count=0;
for(int arr_i=0;arr_i<26;arr_i++)
{
count =abs((count1[arr_i]-count2[arr_i]))+count;
}
return count ;
}
int main() {
char* s1 = (char *)malloc(512000 * sizeof(char));
scanf("%s", s1);
char* s2 = (char *)malloc(512000 * sizeof(char));
scanf("%s", s2);
int result = makingAnagrams(s1, s2);
printf("%d\n", result);
return 0;
}
No comments:
Post a Comment