🎢 ELI5 Park / Arrays & Hashing / Valid Anagram
Easy

Valid Anagram

👶 The 5-Year-Old Summary

Someone gives you two bags of letter magnets. Can you make the same word from both bags? "listen" and "silent" use the same letters - they're anagrams!

🎮 Interactive Visualizer

Click "Step" to compare letter counts!

String 1: "anagram"

String 2: "nagaram"

Letter Counts:

🧠 The Brain Hack

For "is this an anagram?" problems, count letters! Use an array of size 26 (alphabet) or a hash map. +1 for string1, -1 for string2 - all zeros means it's an anagram!

💻 The Clean Solution

def is_anagram(s, t):
    # Quick check: same length?
    if len(s) != len(t):
        return False
    
    # A counter for each letter (a-z)
    count = [0] * 26
    
    # Count letters in first word (+1)
    # Count letters in second word (-1)
    for i in range(len(s)):
        count[ord(s[i]) - ord('a')] += 1
        count[ord(t[i]) - ord('a')] -= 1
    
    # If all zeros, they're anagrams!
    return count == [0] * 26

# Try it!
print(is_anagram("anagram", "nagaram"))  # Output: True
print(is_anagram("rat", "car"))           # Output: False

Time: O(n) - We walk through both strings once!

Space: O(1) - Fixed array of 26 letters!