Check whether two Strings are Anagram of each other using HashMap in Java
Write a function to check whether two given strings are an Anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different.
For example, “abcd” and “dabc” are an Anagram of each other.
Approach: Hashmaps can also be used to find if any two given strings are anagrams or not, by mapping the characters of each string to individual hashmaps and comparing them together. Implementation:
Strings to be checked are: geeksforgeeks forgeeksgeeks The two strings are anagrams of each other Strings to be checked are: geeksforgeeks geeks The two strings are not anagram of each other
Time Complexity: O(l1 + l2) where l1 and l2 are lengths of strings.
Auxiliary space: O(m1 + m2) where m1 and m2 are numbers of unique characters in each string.
Please suggest if someone has a better solution which is more efficient in terms of space and time.
This article is contributed by Aarti_Rathi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Related Article: Check whether two strings are anagram of each other
Please Login to comment...