Intermediate Level
Implement the areTextsAnagram method that checks whether two texts contain the same characters with the same frequencies.
The input contains two strings first and second. Your task is to check whether both strings are anagrams of each other.
Two strings are anagrams when they contain the same characters with the same frequency, but the order may be different. For example, listen and silent are anagrams.
If the lengths are different, the strings cannot be anagrams.
Otherwise, count characters from the first string and reduce the counts using the second string. If any count becomes invalid or any count remains after processing, return false; otherwise return true.
Pseudocode:
function areTextsAnagram(first, second):
if length(first) != length(second):
return false
frequency = empty map
for each character in first:
frequency[character]++
for each character in second:
if character not in frequency:
return false
frequency[character]--
if frequency[character] < 0:
return false
return true