1Question
You are given two strings s and t. Return true if t is an anagram of s, and false if it is not.
An anagram uses exactly the same letters, the same number of times, but the letters can be in any order. For example, "listen" and "silent" are anagrams.
Both words have 3 a, 1 n, 1 g, 1 r and 1 m. Only the order is different.
“rat” has a t, but “car” has a c instead. The letters are not the same.
The lengths are different, so the letters can never match.
Constraints
Up to 50,000 letters. Sorting both strings is fast enough, but we can do it in O(n).
Only 26 possible letters (a to z). So a tiny array of 26 counters is enough.
What if the strings contain Unicode characters? Use a hash map instead of a fixed array of 26.
2Hints
Open one hint at a time. Try to think before you open the next one.
3Pattern
Character Frequency Count
Count how many of each item, and ignore the order.
When order does not matter, two collections are the same if every item has the same count. A hash map stores letter → count, so each update is one O(1) step:
If all counts are 0 at the end, the two strings are made of the same letters.
How to spot this pattern
- The question says “same letters”, “rearrange” or “anagram”.
- The order of items does not matter, only how many of each.
- The set of possible items is small (like 26 letters) or can be stored in a map.
- You are allowed O(1) or O(n) extra memory.
4Explanation
The big idea: Use one counter called count (letter → number). Do two small jobs:
For every letter in s, do count[ch] += 1.
For every letter in t, do count[ch] −= 1.
Why does this work? If t has exactly the same letters as s, every +1 gets cancelled by one −1. So all counts end at 0.
Why check the length first? If the lengths are different, the counts can never all be 0. Returning false early saves work.
5Brute Force
Sort both strings and compare them.
Idea
If two strings are anagrams, then after sorting their letters they become identical. So we:
- Sort the letters of
s. - Sort the letters of
t. - If the two sorted results are equal, return
True.
def is_anagram_brute(s, t):
if len(s) != len(t): # different lengths can never match
return False
return sorted(s) == sorted(t) # same letters in the same order after sorting
print(is_anagram_brute("anagram", "nagaram")) # True6Optimal Solution
One hash map of letter → count.
def is_anagram(s, t):
if len(s) != len(t):
return False
count = {} # letter -> how many
for ch in s:
count[ch] = count.get(ch, 0) + 1 # add for s
for ch in t:
if count.get(ch, 0) == 0: # t has a letter that s has no more of
return False
count[ch] -= 1 # subtract for t
return True # equal lengths + no extra letter = anagram
print(is_anagram("anagram", "nagaram")) # True7Time and Space Complexity
Let n be the number of items in the array.
Brute force (sorting)
Optimal (frequency count)
How big is the difference?
Approximate number of steps:
| n | Sorting (n log n) | Counting (n) |
|---|---|---|
| 10 | about 33 | 10 |
| 1,000 | about 10,000 | 1,000 |
| 50,000 | about 780,000 | 50,000 |
For the biggest input in this problem, counting does around 15 times less work, and it does not need to copy the strings.