"Listen" and "silent". "Dormitory" and "dirty room". "The eyes" and "they see". Anagrams rearrange exactly the same letters into something new, and they appear in crosswords, word games, pen names, brand naming brainstorms and a surprising number of coding tests.
Checking a short pair in your head is easy. Checking two long phrases, or working out why a near miss does not quite work, is where it gets fiddly. This guide explains the precise definition, the two methods used to test anagrams, the decisions you need to make about spaces and accents, and how to see letter by letter where two phrases differ.
What exactly is an anagram?
Two strings are anagrams when they contain the same letters, each used the same number of times, in any order. Order does not matter; counts do. "Listen" and "silent" each have one each of e, i, l, n, s and t, so they match. "Hello" and "world" do not, because the letters differ.
A few conventions apply to phrase anagrams, just as they do for palindromes:
- Case is ignored. "Dormitory" and "Dirty room" are still anagrams even though one starts with a capital D.
- Spaces are ignored. Phrase anagrams almost never keep the same number of words.
- Punctuation is ignored. Apostrophes and commas do not count as letters.
- Accents are a judgement call. In a French or Spanish puzzle, é and e may or may not count as the same letter. There is no universal rule, so decide based on the game you are playing.
A string is technically an anagram of itself, but that is trivial. A useful checker should flag it separately.
Two ways to test for an anagram
Method 1: sort and compare
Normalise both strings (lowercase, remove spaces and punctuation), sort the letters alphabetically, and compare:
listen -> eilnst
silent -> eilnst (same, so they are anagrams)
This is short to write and easy to reason about, which is why it is the textbook answer.
Method 2: count letters
Build a table of how many times each letter appears in each string and compare the tables. It is slightly more work to write but gives a much more useful answer when the strings are not anagrams, because you can see exactly which letters are extra or missing. It is also efficient for long text, since counting does not need a sort.
| Letter | "Astronomer" | "Moon starer" |
|---|---|---|
| a | 1 | 1 |
| e | 1 | 1 |
| m | 1 | 1 |
| n | 1 | 1 |
| o | 2 | 2 |
| r | 2 | 2 |
| s | 1 | 1 |
| t | 1 | 1 |
Every row matches, so the pair is an anagram.
Check anagrams in your browser
Our Anagram Checker shows both views: the sorted letters and a full letter-count table.
- Open Anagram Checker. Type into First word or phrase and Second word or phrase, or click one of the Try: examples such as Listen / Silent.
- Set the rules with the switches: Ignore case, Ignore spaces and Ignore punctuation are on by default. Turn on Ignore accents if é should count as e and ñ as n.
- Read the verdict: "Yes, these are anagrams!", "No, these are not anagrams.", or a note that the two are identical (a trivial anagram).
- If they are not anagrams, the message tells you which letters the first or second phrase has extra, for example "First has extra: l×1".
- Check the Sorted line to see both sets of letters in alphabetical order side by side.
- Scan the table with Letter, First, Second and Difference columns. Rows that differ are highlighted, so near misses are easy to fix.
The swap button between the two fields exchanges the phrases, which is handy when you want the table from the other side.
Other ways to check
On paper
Cross out letters: for each letter of the first phrase, strike the same letter in the second. If both are fully crossed out at the end, they are anagrams. This is effectively the counting method, and it is how many crossword solvers work.
In a spreadsheet
Spreadsheets can count a letter with =LEN(A1)-LEN(SUBSTITUTE(LOWER(A1),"e","")), and you could build a 26-row table for each phrase. It works but is laborious. For letter frequencies in a longer text, the Letter Frequency Counter does this counting automatically.
In code
In Python, collections.Counter gives the counting method in one line:
from collections import Counter
import re
norm = lambda s: re.sub(r"[^a-z]", "", s.lower())
Counter(norm("Dormitory")) == Counter(norm("Dirty room")) # True
Note that this simple version drops accented letters entirely. Handling non-English text properly requires Unicode-aware normalisation, which is exactly the kind of detail a tool with an explicit Ignore accents switch handles for you.
Tips and pitfalls
Almost-anagrams
The most common reason a pair fails is one repeated letter. "Conversation" and "voices rant on" work; drop one "o" and they do not. The Difference column shows +1 or -1 next to the exact letter.
Hidden characters
Text pasted from documents may contain non-breaking spaces or curly apostrophes. With the ignore options on, these are removed, but if you have switched them off, a stray character can cause a mismatch that is invisible on screen. The table lists spaces as "␣ (space)" so you can spot them. Remove Extra Spaces also helps tidy pasted text.
Different alphabets
Letters from different scripts, such as Latin "a" and Cyrillic "а", look identical but are different characters, so they will never match. This occasionally catches people who copy words from multilingual sources.
Anagram versus palindrome
An anagram compares two different strings. A palindrome is a single string that reads the same reversed. For the latter, use the Palindrome Checker.
Frequently asked questions
What is an anagram?
An anagram is a word or phrase formed by rearranging all the letters of another, using each letter exactly as many times as the original. "Listen" and "silent" are a classic pair.
Are "listen" and "silent" anagrams?
Yes. Both contain one each of e, i, l, n, s and t, just in a different order.
Do spaces count in anagrams?
Normally not. Phrase anagrams such as "Dormitory" and "Dirty room" only work when spaces are ignored, which is the default in our checker.
How do I check if two strings are anagrams in code?
Normalise both strings, then either sort their characters and compare, or count each character and compare the counts. Counting also tells you which letters differ.
Is a word an anagram of itself?
Technically yes, since it uses the same letters. Our checker points this out as a trivial anagram rather than a genuine rearrangement.