Loading section...

Anagram Detection and Character Frequency

Concepts: pyAnagram, pyGroupAnagram, pyCanonicalKey

Anagram problems are the hello-world of frequency counting. They appear in nearly every phone screen at some point. The core insight: two strings are anagrams if and only if they have the same character frequencies. Once you see that, the solution is one Counter comparison. But interviewers escalate this problem in predictable ways, and you need to know the escalation path before it happens to you. Valid Anagram (LeetCode 242) — The Baseline Say this in the interview: 'Two strings are anagrams if and only if they have the same character frequencies. I build a Counter for each string and compare them. Python Counter comparison is O(unique characters), and both Counters have at most 26 keys for lowercase English, so the comparison is effectively O(1). Total time O(n + m) for building the Cou