Valid Anagram
Solve and submit on LeetCode to build your LeetCode profile. Use the hints here when you need guidance.
Problem
Decide whether string t is an anagram of string s, meaning it uses exactly the same characters with the same frequencies, just in a different order.
Examples
Constraints
- • 1 <= s.length, t.length <= 5 * 10^4
- • Lowercase English letters only
Hints & approach
Hint 1
Different lengths mean the answer is immediately false.
Hint 2
Two strings are anagrams exactly when their character counts match.
Hint 3
With only 26 letters, a fixed-size array of counts is enough.
Approachtry the hints first
If the lengths differ, return false. Otherwise use a 26-slot count array: increment for each character of s and decrement for each character of t. If every slot ends at zero, the strings contain identical multisets of characters. For arbitrary Unicode input, swap the array for a hash map.
Time O(n) · Space O(1) (26 counters)
