Given two strings s and t, we must return true if the two strings are anagrams, otherwise return False. An anagram is a string that contains the same number of characters regardless of the order.
Example:
Approach 1:
Using a hash map is a great solution. We can first check if the two strings have the same number of characters, if not then we know they can’t possibly be anagrams. If so we iterate over each string and map each string, giving it a key value pair. Each character starts at 0 and increments by 1 every time its seen again until we’ve reach the end of both strings. Finally, we return the boolean mapS == mapT which will either return True or False.
Implementation
Complexity Analysis
Time Complexity: O(N)
Space Complexity: O(1) - Since we can only have characters as input, the worst case scenario for the number of unique characters would be 26.