Anagrams and Palindromes, Explained

After reading this you will know exactly what makes two phrases anagrams, what makes a text a palindrome, how a checker decides, and why the answer changes when you count accents, spaces and punctuation differently.

What the two tests actually ask

An anagram test and a palindrome test look similar because both ignore spaces and case, but they ask different questions.

Anagram
Two texts are anagrams if one is a rearrangement of the other, using every letter the same number of times. Order does not matter. Only the multiset of letters matters.
Palindrome
A single text is a palindrome if it reads the same forwards and backwards after you strip spaces, case and punctuation. Order matters completely; it must be a mirror image.

The classic anagram pair is dormitory and dirty room. Drop the space and lowercase both, and each contains the same nine letters: d, o, r, m, i, t, o, r, y against d, i, r, t, y, r, o, o, m. Sort them and both become dimoorrty. The classic palindrome is A man, a plan, a canal: Panama! Strip everything but letters and lowercase it and you get amanaplanacanalpanama, 21 characters that read identically in reverse.

When to use each test, and when not to

Use the anagram test when you care only about letter content: verifying a word-puzzle answer, checking that a rearrangement uses no extra tiles, or confirming two strings are permutations of each other. Use the palindrome test when position matters: checking a name, a number sequence, or a puzzle phrase that must mirror itself.

Neither test is meaningful for judging spelling, grammar or meaning. listen and silent are perfect anagrams, and so are listen and tinsel. The test confirms letter equality, nothing more.

Both tests here follow puzzle convention: case is ignored, and spaces and punctuation are removed before comparing. That is why A man, a plan... counts as a palindrome even though the raw string, with its commas and capital, is not literally reversible.

The rule behind each test

For an anagram, count how many times each letter appears in each phrase. Two phrases are anagrams if and only if every letter count matches. Let c_A(x) be the number of times letter x appears in phrase A after cleaning, and c_B(x) the count in phrase B.

\text{anagram} \iff \forall x : c_A(x) = c_B(x)

Here x ranges over every distinct letter that appears in either phrase, and \forall means the equality must hold for all of them. If a single letter is off by one, the answer is no.

For a palindrome, clean the text to a string s of length n, indexed from 0 to n-1. It is a palindrome when each character equals its mirror partner.

\text{palindrome} \iff \forall i : s_i = s_{n-1-i}

You only need to check i from 0 up to \lfloor n/2 \rfloor - 1, because once you pass the middle the pairs repeat. A string of 21 characters needs 10 comparisons; a string of 20 needs 10 as well.

Reproducing the demo data

The demo checks dormitory against dirty room for the anagram, and A man, a plan, a canal: Panama! for the palindrome.

  1. Clean the anagram inputs. dormitory is already 9 letters. dirty room loses its space and becomes dirtyroom, also 9 letters. Equal length is a necessary first check.
  2. Count letters in dormitory: d 1, o 2, r 2, m 1, i 1, t 1, y 1. That is 7 distinct letters totalling 9.
  3. Count letters in dirtyroom: d 1, i 1, r 2, t 1, y 1, o 2, m 1. The same seven counts. Every c_A(x) = c_B(x), so the phrases are anagrams.
  4. Clean the palindrome input. Keep only letters and lowercase: amanaplanacanalpanama, length n = 21.
  5. Compare mirror pairs. Position 0 is a, position 20 is a. Position 1 is m, position 19 is m. Continue inward. Position 10, the center, is c and pairs with itself. All 10 pairs match, so it is a palindrome.

Reading a "no" answer

When two phrases are not anagrams, the useful information is the difference between their letter counts. Subtract one count from the other for each letter. A positive number means the second phrase has extra copies; a negative number means it is missing some.

Compare dormitory with dirty rooms (note the trailing s). The second phrase now has 10 letters against 9, so it cannot be an anagram. The letter table shows the single culprit.

Letter counts for dormitory versus dirtyrooms
LetterdormitorydirtyroomsDifference
d110
i110
m110
o220
r220
s01+1
t110
y110

Only the row for s is nonzero. The checker reports one extra letter, so you know precisely what to remove or where the puzzle went wrong.

Every pair of bars matches except at s, where the second phrase stands one higher. That single mismatch is why the pair fails the anagram test.

Try changing one letter

The palindrome test is easy to break and easy to feel. Change one character and watch how many mirror pairs stop matching.

Take the cleaned string racecar (length 7). Its pairs are (r,r), (a,a), (c,c) around the center e. All three pairs match, so it is a palindrome. Change the final r to a t and the outer pair becomes (r,t): one pair fails, so the whole test fails.

Common mistakes

The most frequent surprise is accented letters. This checker compares characters as typed. café and face are not anagrams here, because é is a different character from e. If your puzzle treats é as e, strip the accents first.

Invisible characters cause false negatives. A trailing zero-width space or a non-breaking space (codepoint U+00A0) is not a normal space, so it may not be stripped and will throw off both tests. If a pair looks right but fails, inspect the raw codepoints with the Unicode Inspector.

A second trap is punctuation that carries meaning. In numbers, a comma or period is not decoration. The checker removes them for the palindrome test, so 12.21 and 1221 both reduce to the same digits and both read as palindromes. If you need the dot to count, that is a different question than the one this tool answers.

Third, remember that anagram counting includes duplicated letters. aab and abb have the same three letters loosely speaking, but the counts differ: a is 2 versus 1. They are not anagrams. The multiset, not the set, is what must match.

Related tools

If your input has hidden characters that break either test, open the Unicode Inspector to see every codepoint. To count letters, words and characters in a longer passage, use the Word & Character Counter. When two phrases fail the anagram test and you want a side-by-side view of what differs, the Text Diff Checker highlights the changes. For letter and word patterns across a whole document, the Word Frequency & N-gram Analyzer shows the most common tokens, and the Readability Scorer rates how hard the text is to read.

Frequently asked questions

Do spaces and capital letters matter?

No. Both tests lowercase the text and remove spaces and punctuation before comparing, following puzzle convention. That is why Dirty Room and dormitory still register as anagrams.

Is every anagram also a palindrome?

No, and the two are independent. dirty room is an anagram of dormitory but reads as moorytrid reversed, which is not the same, so it is no palindrome. A palindrome is trivially an anagram of itself, but that tells you nothing useful.

Why do café and cafe give different answers?

The character é (codepoint U+00E9) is not the letter e (codepoint U+0065). The checker compares codepoints, so the counts differ. Remove the accent first if your puzzle treats them as the same letter.

Can a number be a palindrome?

Yes. After stripping non-letters, digits are compared the same way as characters. 13531 reads the same in reverse, so it passes. Note that any commas or periods are removed first, so 1,331 becomes 1331.

How many comparisons does the palindrome test do?

Half the length, rounded down. A cleaned string of 21 characters needs 10 pair comparisons; a string of 100 needs 50. The middle character of an odd-length string never needs checking, since it mirrors onto itself.