Lesson 18 — Anagram check
Anagrams: compare letter counts
Two words are anagrams if they use exactly the same letters the same number of times ("listen" / "silent"). Instead of sorting, count each letter in a dict and compare the two dicts. d.get(key, 0) returns the stored value, or 0 if the key is new.
counts = {}
counts["a"] = counts.get("a", 0) + 1 # {'a': 1}
Counting is O(n); sorting is O(n log n).
Why it matters: "count with a dict, then compare" solves a whole family of problems: duplicates, most common item, matching inventories.
Finish letter_counts(word) by counting each character. Expected: True, False, True.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based Algorithms Foundations courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for Algorithms Foundations books — check the publication year so the examples match current versions.
Search Amazon →