See the original problem on HackerRank.
Solutions
Two strings are anagrams of one another if they share the same characters and each character has the same frequency in both strings. Thus, we can easily solve this problem with a frequency table.
Basically, we can add 1
for each character in a
and subtract 1
for each character in b
. Those characters with non-zero frequency must be deleted and then added to the total count.
Here is an implementation in C++:
|
|
The reduce pattern brings out thus the third for loop can be replaced with:
|
|
Just for fun, here is a possible implementation of the same call to accumulate
with C++20 ranges:
|
|