Valid Anagram
The problem
Given two lowercase English strings, determine whether their character counts are identical. The order of the characters does not matter.
Example
s = "listen", t = "silent" → true
Need a hint?
Equal lengths are necessary, but not sufficient.
Write pseudocode, trace the example, or note an edge case. This scratchpad does not run code.
Notes stay in this browser when storage is available.
Read the solution approach
Reject unequal lengths. Increment a 26-entry frequency array for the first string and decrement it for the second. The strings are anagrams exactly when every count is zero. A map generalizes the approach to a larger alphabet.
Complexity
O(n) time and O(1) auxiliary space for a fixed alphabet.
Before moving on, explain why the algorithm is correct and trace a boundary case without looking at the approach.