Anagram Detector
Level: Beginner 15–30 minConcepts: AlgorithmsStringsValidation
Solutions: C# | TypeScript | Python
Build an anagram detection system that can check pairs, filter lists, and group words by their anagram sets.
Requirements
Step 1: Pair Detection
Given two words, determine if they are anagrams of each other.
- An anagram uses exactly the same letters, the same number of times
- Comparison should be case-insensitive
- Spaces and punctuation are ignored
- A word is not an anagram of itself
Examples:
"listen"and"silent"→ true"hello"and"world"→ false"Astronomer"and"Moon starer"→ true"rail safety"and"fairy tales"→ true"cat"and"cat"→ false (same word)
Step 2: Find Anagrams in a List
Given a subject word and a list of candidates, return the candidates that are anagrams of the subject.
subject: "listen"
candidates: ["enlists", "google", "inlets", "banana", "silent"]
result: ["inlets", "silent"]
Step 3: Group Anagrams
Given a list of words, group them by their anagram sets.
input: ["eat", "tea", "tan", "ate", "nat", "bat"]
output: [["eat", "tea", "ate"], ["tan", "nat"], ["bat"]]
Test Cases
Pair detection:
| Word A | Word B | Anagram? |
|---|---|---|
"listen" | "silent" | true |
"hello" | "world" | false |
"cat" | "tac" | true |
"cat" | "cat" | false |
"Cat" | "tac" | true |
"" | "" | false |
"a" | "a" | false |
"ab" | "ba" | true |
"aab" | "abb" | false |
Find anagrams:
| Subject | Candidates | Result |
|---|---|---|
"listen" | ["silent", "tinsel"] | ["silent", "tinsel"] |
"listen" | ["hello", "world"] | [] |
"master" | ["stream", "maters", "pigeon"] | ["stream", "maters"] |
Group anagrams:
| Input | Groups |
|---|---|
["eat", "tea", "ate"] | [["eat", "tea", "ate"]] |
["abc", "def"] | [["abc"], ["def"]] |
[] | [] |
Bonus
- Find the longest anagram pair in a dictionary file
- Detect partial anagrams — words where one is an anagram of a substring of the other
- Generate all possible anagrams of a given word (permutations that are real words, given a dictionary)
Hint
The key insight: two words are anagrams if they have the same sorted characters. Sorting the letters of "listen" and "silent" both give "eilnst". This sorted form is the anagram “key” — use it for grouping and comparison.
Reference Walkthrough
Reference implementations in C#, TypeScript, and Python live at tddbuddy-reference-katas/anagram-detector. This is an F1 kata — 18 scenarios across three pure functions (pair detection, find-in-list, group), shared across all three languages, built around a single sorted-letters “anagram key” helper.
- C# (.NET 8, xUnit, FluentAssertions 6.12.0) — walkthrough
- TypeScript (Node 20, Vitest 1.6, TS 5 strict) — walkthrough
- Python (3.11, pytest) — walkthrough
This kata ships in Agent Full-Bake mode at high gear: the algorithm is small enough to land as one commit per language, with a brief walkthrough noting there are no builders because the algorithm’s inputs and outputs are the domain. Strings and lists of strings go in, booleans and lists of strings come out — no aggregates to construct, no value types to introduce, no collaborators to inject. See the repo’s Gears section for when high gear is the right call.
Related reading
- Examples Pin Intent. Properties Pin the Invariants.
Example-based tests anchor scenarios in the team's vocabulary. Property-based tests anchor invariants across the input space. The agent needs both axes, and most suites only carry one. - Katas Are Rehearsal, Not Performance
You don't practice TDD on production code. You practice on katas and bring the muscle memory to production. The gap between knowing TDD and doing TDD is reps. - The Test Pyramid Was an Economic Argument
The test pyramid was not a quality law. It was a cost structure: unit tests were cheap, integration tests were expensive, so you wrote many of the first and few of the second. Agents collapsed the cost of writing tests at every level, and the cheapest test that still tells the truth is the one that pins a seam the agent cannot fake.