Given two strings s and t, return true if the two strings are anagrams of each other, otherwise return false.
An anagram is a string that contains the exact same characters as another string, but the order of the characters can be different.
Example 1:
Input: s = "racecar", t = "carrace"
Output: trueExample 2:
Input: s = "jar", t = "jam"
Output: falseConstraints:
s and t consist of lowercase English letters.
You should aim for a solution with O(n + m) time and O(1) space, where n is the length of the string s and m is the length of the string t.
A brute force solution would be to sort the given strings and check for their equality. This would be an O(nlogn + mlogm) solution. Though this solution is acceptable, can you think of a better way without sorting the given strings?
By the definition of the anagram, we can rearrange the characters. Does the order of characters matter in both the strings? Then what matters?
We can just consider maintaining the frequency of each character. We can do this by having two separate hash tables for the two strings. Then, we can check whether the frequency of each character in string s is equal to that in string t and vice versa.
Before attempting this problem, you should be comfortable with:
If two strings are anagrams, they must contain exactly the same characters with the same frequencies.
By sorting both strings, all characters will be arranged in a consistent order.
If the two sorted strings are identical, then every character and its count match, which means the strings are anagrams.
false immediately because they cannot be anagrams.true.false.Where is the length of string and is the length of string .
If two strings are anagrams, they must use the same characters with the same frequencies.
Instead of sorting, we can count how many times each character appears in both strings.
By using two hash maps (or dictionaries), we track the frequency of every character in each string.
If both frequency maps match exactly, then the strings contain the same characters with same frequencies, meaning they are anagrams.
false immediately.s[i] in the first map.t[i] in the second map.true.false.Where is the length of string and is the length of string .
Since the problem guarantees lowercase English letters, we can use a fixed-size array of length 26 to count character frequencies instead of a hash map.
As we iterate through both strings simultaneously, we increment the count for each character in s and decrement the count for each character in t.
If the strings are anagrams, every increment will be matched by a corresponding decrement, and all values in the array will end at 0.
This approach is efficient because it avoids hashing and uses constant space.
false immediately.count of size 26 initialized to 0.s[i].t[i].count array:0, return false because the frequencies differ.0, return true since the strings are anagrams.Where is the length of string and is the length of string .
If two strings have different lengths, they cannot be anagrams. Skipping this early check means wasting time processing strings that could never match. Always compare lengths first and return false immediately if they differ.
When the problem specifies lowercase letters only (as in this problem), case sensitivity is not an issue. However, if the problem allows mixed case, forgetting to normalize to the same case (e.g., converting both strings to lowercase) will cause incorrect results where "Listen" and "Silent" would wrongly be considered non-anagrams.