-
Notifications
You must be signed in to change notification settings - Fork 2
/
char_del_anagrams.cpp
52 lines (41 loc) · 1.01 KB
/
char_del_anagrams.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
Given two strings S1 and S2 consisting of only lowercase characters whose anagrams are almost equal.
The task is to count the number of characters which needs to be edited(delete) to make S1 equal to S2.
Input:
1
madame
madam
Output:
1
Explanation:
Testcase 1: String S1 = madame, string S2 = madam.
character 'e' in first string needs to be deleted to make S1 equal to S2.
*/
#include <bits/stdc++.h>
using namespace std;
// Function Prototype
int countChars(string, string);
int main() {
int testcase;
cin >> testcase;
while(testcase--){
string s1, s2;
cin >> s1 >> s2;
cout << countChars(s1, s2) << endl;
}
return 0;
}
int countChars(string s1, string s2){
map<int, int> m1, m2;
for(int i = 0; i < s1.length(); i++) {
m1[s1[i] - 'a']++;
}
for(int i = 0; i < s2.length(); i++) {
m2[s2[i] - 'a']++;
}
int ans = 0;
for(int i = 0; i < 26; i++) {
ans += abs(m1[i] - m2[i]);
}
return ans;
}