Overview
Two strings are anagrams if they contain exactly the same characters in any order.
Analogy
'Listen' and 'Silent' use the same letters rearranged.
Step-by-step
- Sort both strings and compare: sorted(a) == sorted(b).
- Or use Counter: Counter(a) == Counter(b).
- Fastest: count array of 26 if lowercase ASCII only.
Visual
'anagram'→sorted→'aaagmnr'; 'nagaram'→sorted→'aaagmnr' → equal
Common mistakes
- Forgetting to normalize case.
- Not handling spaces/punctuation if the problem expects cleaned input.
Practice questions
- Check if two sentences are anagrams ignoring spaces.
- Find all anagram groups in a list of words.
Time
O(n log n) sort, O(n) counter