Back to questions
Given two strings and , return if the two strings are anagrams of each other, otherwise return .
An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once.
You can assume both of the strings will contain lowercase alphabet characters.
Input:
Output:
Input:
Output:
Input:
Output:
If two strings are anagrams, the only difference is the arrangement of their characters.
To verify this, we can sort both strings. If they are anagrams, their sorted versions will be identical.
We can sort the strings in two ways:
Here's the implementation using the second approach:
If two strings are anagrams, each character should appear the same number of times in both strings.
Thus, we can count the frequency of each character and compare the counts.
Instead of using two dictionaries, we can use one: