Community Problem
Valid Anagram
Difficulty: Easy
Decide whether two strings are anagrams of each other in O(n) using a frequency map.
Valid Anagram
Decide whether two strings are anagrams of each other in O(n) using a frequency map.
By @ezb1981
March 22, 2026
·
Updated May 18, 2026
381 views
7
3.8 (11)
Two strings are anagrams when they contain the exact same letters with the exact same frequencies, possibly rearranged. "listen" and "silent" are anagrams; "hello" and "world" are not. This is the gateway problem for hash-map frequency counting and one of the most common warm-up questions in a phone screen.
Valid Anagram
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
Examples
Example 1:
- Input:
s = "anagram",t = "nagaram" - Output:
true
Example 2:
- Input:
s = "rat",t = "car" - Output:
false
Example 3:
- Input:
s = "",t = "" - Output:
true
Constraints
0 <= s.length, t.length <= 5 * 10^4sandtconsist of lowercase ASCII letters only.
Follow-up
What if the inputs contained Unicode characters? Does your O(1)-extra-space-with-fixed-alphabet approach still hold up, or do you need to fall back to a hash map?
