Anagram of String (Function Problem)

Given two strings in lowercase, the task is to make them anagram. The only allowed operation is to remove a character from any string. Find minimum number of characters to be deleted to make both the strings anagram. If two strings contains same data set in any order then strings are called Anagrams.

Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of 2 strings to make the anagrams.

Output:
Output the minimum number of characters to be deleted to make both the strings anagram.

Constraints:
1<=T<=10^5
1<=length of string<=10^5

Example:
Input:

2
bcadeh
hea
cddgk
gcd

Output:
3
2


/*
Please note that it's Function problem i.e.
you need to write your solution in the form Function(s) only.
Driver Code to call/invoke your function would be added by GfG's Online Judge.*/

*/
// function to calculate minimum numbers of characters
// to be removed to make two strings anagram
int remAnagram(string str1, string str2)
{
int arr[26];
int count=0;
memset(arr,0,sizeof(arr));
/*if(strcmp(str1,str2) == 0)
return 0;*/
for(int i=0;i<str1.size();i++)
{
arr[str1[i]-'a'] = arr[str1[i]-'a']+1;
//cout<<str1[i]-'a'<<endl;
}
//int count=0;
for(int i=0;i<str2.size();i++){
if(arr[str2[i]-'a'] == 0)
count++;
else
arr[str2[i]-'a'] = arr[str2[i]-'a']-1;
}
for(int i=0;i<26;i++)
{
count +=arr[i];
}
return count;
}
 

Post a Comment

0 Comments