Valid Anagram easy
Description
Given two strings s and t, return true if t is an anagram of s — i.e., uses exactly the same characters with the same counts.
Examples
> Case 1:
s = "anagram", t = "nagaram"
Output: true
> Case 2:
s = "rat", t = "car"
Output: false # different characters
> Case 3:
s = "aab", t = "abb"
Output: false # 'a' appears 2 in s but only 1 in tConstraints
1 <= s.length, t.length <= 5 * 10^4sandtconsist of lowercase English letters only.- Follow-up: what if the input contains Unicode?
Approach 1: Sort and compare — O(n log n)
The one-liner solution. Two strings are anagrams iff their sorted forms match.
Time O(n log n), space O(1) extra (in-place sort). Fine for the constraints, but we can do better.
Approach 2: Count once — O(n)
For lowercase English, the alphabet has 26 letters. Use a 26-element array as a fixed-size hash map.
The trick: walk both strings simultaneously, incrementing for s and decrementing for t. If at the end any count is non-zero, the strings differ.
Time O(n), space O(1) — the 26-element array doesn’t grow with input. This is the textbook winning answer.
The “increment for one, decrement for the other” trick is a tiny optimization that lets you do a single sweep instead of two — but more importantly, it’s a useful pattern: encode “matched” as a counter that returns to zero. Same trick appears in valid-parentheses checks (push on (, pop on )).
Follow-up: Unicode
For unicode, the 26-slot array breaks — there are over a million code points. Switch to a hash map:
from collections import Counter
def is_anagram_unicode(s, t):
return Counter(s) == Counter(t)Same logic, just with a Counter (which is a dict under the hood). Still O(n) time.
For most non-Unicode inputs the 26-slot array beats the hash map on constant factor, because no hashing is needed.
Why this matters as a building block
Valid Anagram looks trivial. But the same character-count signature that solves it powers:
| Problem | Twist |
|---|---|
| Group Anagrams | Use the signature as a hash-map key. |
| Find All Anagrams in a String | Slide a window over s, comparing window counts to pattern counts. |
| Substring with Concatenation of All Words | Generalized to multi-word matching. |
| Permutation in String | Same as Find All Anagrams, return bool only. |
This is why Valid Anagram is one of the most-asked easy problems — it’s the gateway to a family of medium and hard problems.
Analysis
| Approach | Time | Space |
|---|---|---|
| Sort | O(n log n) | O(1) |
| Count array | O(n) | O(1) (k=26 buckets) |
| Hash counter | O(n) | O(k) (k distinct chars) |