MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / makeAnagram

Function makeAnagram

Hackerrank_problems/Making Anagrams/solution.cpp:6–41  ·  view source on GitHub ↗

Complete the makeAnagram function below.

Source from the content-addressed store, hash-verified

4
5// Complete the makeAnagram function below.
6int makeAnagram(string a, string b)
7{
8 int i, cut = 0;
9 set<char> s;
10 int maiorS = max(a.size(), b.size()); //Create a variable to storage the len of the bigger string
11 int menorS = min(a.size(), b.size()); //Create a variable to storage the len of the smaller string
12
13 // We have a for and 2 ifs above below, i'm storing these char, from string a and string b in a set called s;
14
15 for (i = 0; i < menorS; i++){
16 s.insert(a[i]);
17 s.insert(b[i]);
18 }
19
20 if (a.size() > b.size()){
21 for (; i < maiorS; i++){
22 s.insert(a[i]);
23 }
24 }
25
26 if (b.size() > a.size()){
27 for (; i < maiorS; i++){
28 s.insert(b[i]);
29 }
30 }
31
32 // Now the set s have all the char occurrences, and now we will count the occurrences of each string
33
34 for (auto let : s){
35 int ocurA = count(a.begin(), a.end(), let);
36 int ocurB = count(b.begin(), b.end(), let);
37 cut += abs(ocurA - ocurB); // The int cut stores the differences of occurrences on each string, and compare the difference between these two occurrences
38 }
39 // Now we have the number of same occurrences between string a and b
40 return cut;
41}
42
43int main(){
44 ofstream fout(getenv("OUTPUT_PATH"));

Callers 1

mainFunction · 0.85

Calls 4

maxFunction · 0.85
minFunction · 0.85
absFunction · 0.85
insertMethod · 0.45

Tested by

no test coverage detected