Counting Palindromes

Combinatorics · Easy · Free problem

Using a 26-letter alphabet, how many 3-letter palindromes are there? How many 5-letter palindromes?

(A palindrome reads the same forwards and backwards, e.g. 'aba' or 'level'.)

Hints

  1. A palindrome is fully determined by its first half; the second half is just a mirror.
  2. Count the number of freely-choosable positions: $\lceil n/2 \rceil$ for a length-$n$ string.
  3. Raise the alphabet size to that power:
6^2$ for length 3, 6^3$ for length 5.

Worked Solution

How to Think About It: A palindrome is determined entirely by its first half -- the back half is forced to mirror the front. So just count the free positions and raise 26 to that power.

Quick Estimate: For 3 letters, the middle letter is free and the first determines the last, so two free choices: 6^2$. For 5 letters, the first three positions are free and the last two mirror them: 6^3$.

Formal Solution: A length-$n$ palindrome over an alphabet of size 6$ is fixed by its first $\lceil n/2 \rceil$ characters. - For $n = 3$: free positions are 1 and 2 (position 3 mirrors position 1), so 6^2 = 676$. - For $n = 5$: free positions are 1, 2, 3 (positions 4 and 5 mirror 2 and 1), so 6^3 = 17{,}576$.

Answer: $676$ three-letter palindromes and

7{,}576$ five-letter palindromes.

Intuition

This is a pure mental-math counting check, and the entire trick is realizing that the mirror constraint removes degrees of freedom. The count is 6^{\lceil n/2 \rceil}$ because exactly half (rounded up) of the positions are free and the rest are determined. Interviewers use this to see whether you immediately reduce the problem to 'how many independent choices do I make' rather than trying to enumerate.

The same degrees-of-freedom counting underlies more serious work: counting the free parameters of a symmetric matrix, the dimension of a constrained space, or the number of independent entries after imposing structure. Whenever a symmetry or constraint ties some choices to others, the right count is over the free choices only.

Open the full interactive solver →