0
0
DSA Pythonprogramming

Anagram Check Techniques in DSA Python

Choose your learning style9 modes available
Mental Model
Two words are anagrams if they have the exact same letters in the same amounts, just mixed up.
Analogy: Imagine two bags of colored beads. If both bags have the same colors and the same number of each color, they are anagrams, even if the beads are arranged differently.
word1: c -> a -> t -> null
word2: t -> a -> c -> null

We want to check if these two chains have the same beads (letters) in any order.
Dry Run Walkthrough
Input: word1: 'listen', word2: 'silent'
Goal: Check if 'listen' and 'silent' are anagrams by comparing their letter counts.
Step 1: Count letters in 'listen': l=1, i=1, s=1, t=1, e=1, n=1
counts1 = {'l':1, 'i':1, 's':1, 't':1, 'e':1, 'n':1}
Why: We need to know how many times each letter appears in the first word.
Step 2: Count letters in 'silent': s=1, i=1, l=1, e=1, n=1, t=1
counts2 = {'s':1, 'i':1, 'l':1, 'e':1, 'n':1, 't':1}
Why: We count letters in the second word to compare with the first.
Step 3: Compare counts1 and counts2: all letters and counts match
counts1 == counts2 -> True
Why: If counts match exactly, words are anagrams.
Result:
'listen' and 'silent' are anagrams
Annotated Code
DSA Python
from collections import Counter

class AnagramChecker:
    def are_anagrams(self, word1: str, word2: str) -> bool:
        # Count letters in first word
        counts1 = Counter(word1)
        # Count letters in second word
        counts2 = Counter(word2)
        # Compare both counts
        return counts1 == counts2

if __name__ == '__main__':
    checker = AnagramChecker()
    word1 = 'listen'
    word2 = 'silent'
    result = checker.are_anagrams(word1, word2)
    print(f"'{word1}' and '{word2}' are anagrams" if result else f"'{word1}' and '{word2}' are NOT anagrams")
counts1 = Counter(word1)
count letters in first word to know frequency
counts2 = Counter(word2)
count letters in second word to compare frequencies
return counts1 == counts2
check if both words have exactly same letter counts
OutputSuccess
'listen' and 'silent' are anagrams
Complexity Analysis
Time: O(n) because we count letters in both words once, where n is the length of the words
Space: O(1) because the letter counts use fixed space limited by alphabet size
vs Alternative: Sorting both words takes O(n log n) time, so counting letters is faster and more efficient
Edge Cases
empty strings
Both empty strings are considered anagrams
DSA Python
return counts1 == counts2
different lengths
Letter counts differ, so not anagrams
DSA Python
return counts1 == counts2
same letters, same counts but different order (e.g. 'aabb' and 'abab')
Counts match, so they are anagrams
DSA Python
return counts1 == counts2
When to Use This Pattern
When you need to check if two words have the same letters in any order, use letter counting to quickly compare frequencies.
Common Mistakes
Mistake: Comparing sorted strings without considering case sensitivity
Fix: Convert both words to the same case (e.g., lower) before counting or sorting
Mistake: Not handling empty strings or different lengths explicitly
Fix: Counting letters naturally handles these cases, so rely on counts equality
Summary
Checks if two words have the same letters in the same amounts regardless of order.
Use when you want to verify if two strings are anagrams efficiently.
Counting letter frequencies and comparing them is the key insight for fast anagram checking.