-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathanagram2.cpp
37 lines (32 loc) · 1.05 KB
/
anagram2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// C++ program to find minimum number of characters
// to be removed to make two strings anagram.
#include<bits/stdc++.h>
using namespace std;
const int CHARS = 26;
// function to calculate minimum numbers of characters
// to be removed to make two strings anagram
int remAnagram(string str1, string str2)
{
// make hash array for both string and calculate
// frequency of each character
int count1[CHARS] = {0}, count2[CHARS] = {0};
// count frequency of each charcter in first string
for (int i=0; str1[i]!='\0'; i++)
count1[str1[i]-'a']++;
// count frequency of each charcter in second string
for (int i=0; str2[i]!='\0'; i++)
count2[str2[i]-'a']++;
// traverse count arrays to find number of charcters
// to be removed
int result = 0;
for (int i=0; i<26; i++)
result += abs(count1[i] - count2[i]);
return result;
}
// Driver program to run the case
int main()
{
string str1 = "bcadeh", str2 = "hea";
cout << remAnagram(str1, str2);
return 0;
}