Description:
Given two strings str1 and str2, return true if str2 is an anagram of str1, and false otherwise.
Note: An Anagram is defined as a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once.
Example 1: Input: str1 = "anagram", str2 = "nagaram" Output: true
Example 2: Input: str1 = "rat", str2 = "cat" Output: false
Algorithmic Steps This problem is solved with the help of frequency counting approach using an array.
- If the strings are not the same length, return
false. - Create an array of size 26 to count letter frequencies.
- For each character in both strings, increment for
str1and decrement forstr2. - If all counts are zero, the strings are anagrams.
Note:There are other approaches as well to validate anagrams
- If the strings are not the same length, return
false. - Count the frequency of each character in
str1using a map. - Decrement the count for each character in
str2. - If any count goes negative or a character is missing, return
false. - If the map is empty at the end, the strings are anagrams.
- If the strings are not the same length, return
false. - Sort both strings and compare them.
Time and Space complexity:
This algorithm takes a time complexity of O(n), where n represents length of the string. This is because we are traversing the string at most once.
Also, it requires space complexity of O(1)(i.e, O(26)) due to no other data structure used except fixed 26 letter array for the character frequencies.
The complexity of all approaches can be summarized as below,
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Frequency Array | O(n) | O(1) |
| Hash Map | O(n) | O(n) |
| Sort & Compare | O(n log n) | O(n) |