Valid Anagram
Two strings are anagrams iff their character counts match.
Approach
Count characters in the first string, then decrement for the second; if every count ends at zero (and lengths match), they're anagrams. A fixed-size array of 26 counts is ideal for lowercase input. Sorting both strings also works but is O(n log n).
Time complexity
O(n)
Space complexity
O(1) for a fixed alphabet
Common mistake
Forgetting to check lengths first — differing lengths can never be anagrams and save you the count.
See it run, step by step
Generate an interactive lesson for Valid Anagram — trace every variable and watch the algorithm execute until it clicks.
Start a lesson on Valid Anagram