For example ‘listen’ is an anagram of ‘silent’ and ‘enlist’.
We have to write a function to check if two given strings are anagrams are not.
We can solve this problem in two different ways. The first way is to sort the two strings and compare them. If they are equal, they are anagrams; otherwise they are not. The intuition behind this is by sorting the letters in the string, it forms a unique pattern. All anagrams must transform to this unique pattern when they are sorted.
For example when we sort the letters in the string ‘listen’ or ‘silent’ it becomes
Here is the Java function which implements the first method. Here I have used separate strings to hold the sorted sequence so that the original strings are not modified. If the original strings can be modified, we can avoid using the extra String variables.
The second method is to count the letters in both the strings and match the counts. If the letter count of both the strings is same, then we can conclude that they are anagrams.
In the following Java implementation we do not count the frequencies of letters in both the strings, but we calculate the counts for just one string.
Then we iterate through the characters of the second string, we try to decrement the frequency of each character. If we found a zero, that means that particular character has appeared more number of times in the second string, then we can say that they are not anagrams. Otherwise we decrement the count of that character.
Finally we check the count array if they have any non-zero entries. If they are anagrams, we have all zero entries in count array.
Note: We have considered the space also as a significant character in the above programs. So ‘school master’ can not be anagram of ‘the classroom’ even though they contain same letters. Also the input strings are case-sensitive.