-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakeAnagrams_Hackerrank.cpp
More file actions
55 lines (48 loc) · 1.36 KB
/
MakeAnagrams_Hackerrank.cpp
File metadata and controls
55 lines (48 loc) · 1.36 KB
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
53
54
55
//https://www.hackerrank.com/challenges/ctci-making-anagrams
#include <cmath>
#include <string>
#include <cstdio>
#include <vector>
#include <iostream>
#include <unordered_map>
using namespace std;
int number_needed(string a, string b) {
int length_of_a = a.size();
int length_of_b = b.size();
int larger = length_of_a >= length_of_b ? length_of_a : length_of_b;
int forward = 0;
unordered_map<char,int> count_of_chars ;
while(forward < larger){
if (forward < length_of_a ){
if(count_of_chars.find(a[forward]) == count_of_chars.end()){
count_of_chars[a[forward]] = 1;
}else{
count_of_chars[a[forward]] += 1;
}
}
if (forward < length_of_b ){
if(count_of_chars.find(b[forward]) == count_of_chars.end()){
count_of_chars[b[forward]] = -1;
}else{
count_of_chars[b[forward]] -= 1;
}
}
++forward;
}
int result = 0;
for(unordered_map<char,int>::const_iterator itr
= count_of_chars.begin(), itr_end = count_of_chars.end();
itr != itr_end; ++itr)
{
result += abs(itr->second);
}
return result;
}
int main(){
string a;
cin >> a;
string b;
cin >> b;
cout << number_needed(a, b) << endl;
return 0;
}