Last updated:

Palindromes, Ambigrams, and Anagrams - The Mathematics of Wordplay Born from Character Count

10 min read

Read "racecar" backwards and it's still "racecar." Behind this simple trick lies deep mathematics involving combinatorics, computational complexity theory, and the structure of language. Palindromes, anagrams, pangrams, lipograms. These wordplay forms, born from constraints on letter arrangement and character count, are staples of programming contests, foundations of cryptography, and above all, intellectual games that push the limits of human linguistic ability.

Palindromes - The Same Forwards and Backwards

A palindrome is a text that reads the same forwards and backwards. In Japanese, famous examples include "shinbunshi" (newspaper) and "tomato." In English, classics like "racecar," "madam," and "A man, a plan, a canal: Panama!" are well known.

Japanese palindromes carry a disagreement that English does not have: how to treat dakuten (voicing marks) and handakuten (semi-voicing marks). When a symmetric position pairs a plain kana with its voiced counterpart, such as "ka" and "ga," the lenient position treats them as the same character and accepts the text as a palindrome, while the strict position does not. The frequently cited "takeyabu yaketa" (the bamboo grove burned) becomes "ta-ke-ya-bu-ya-ke-ta" once written in kana, and those seven characters are already symmetric on their own, so it holds without relying on that leniency at all. What actually changes answer with the criteria are works whose symmetric positions disagree over whether a voicing mark is present. This "looseness" widens the expressive range of Japanese palindromes, but from the perspective of Japanese text rules, it means the same work can be counted as a palindrome or not unless the criteria are fixed in advance.

Palindromes are not limited to short phrases. Some writers have produced full-length novels in which the entire text reads the same in either direction. Two English works are widely known: "Satire: Veritas" by David Stephens (1980, 58,795 characters) and "Dr Awkward & Olson in Oslo" by Lawrence Levine (1986, 31,954 words). Keeping tens of thousands of words symmetric from end to end forces the naturalness of the prose to be sacrificed, so readability as a piece of writing and the severity of the constraint pull directly against each other.

Palindrome Detection Algorithms - The Elegance of O(n)

In programming, the algorithm for determining whether a string is a palindrome is an ideal introduction to computational complexity theory. The simplest method is to reverse the string and compare it with the original. This achieves O(n) time complexity and O(n) space complexity.

A more efficient approach places two pointers at both ends of the string and compares characters one by one toward the center. This method achieves O(n) time complexity with O(1) space complexity.

Manacher's algorithm can detect all palindromic substrings in a string in O(n) time. Published by Glenn Manacher in 1975, this algorithm efficiently computes the radius of the longest palindrome centered at each position. The core idea is reducing an O(n²) naive approach to O(n) by leveraging the symmetry of previously computed palindromes.

AlgorithmTime ComplexitySpace ComplexityFeatures
String reversal + comparisonO(n)O(n)Simplest implementation
Two-pointer methodO(n)O(1)No additional memory needed
Recursive checkO(n)O(n) (stack)Suited for functional programming
Longest palindromic substring (Manacher)O(n)O(n)Detects all palindromic substrings
Palindrome partitioning (DP)O(n²)O(n²)Finds minimum partition count

In programming contests (AtCoder, LeetCode, etc.), palindrome problems appear frequently. LeetCode's "Longest Palindromic Substring" is a classic interview question. The pattern matching knowledge covered in regex character count and design can also be applied to palindrome detection.

Japanese palindrome detection requires additional preprocessing: normalizing dakuten and handakuten, handling the long vowel mark "ー," and deciding whether to treat contracted sounds (like "kyo") as one or two characters. These rules vary across palindrome communities, and no unified standard exists. When programmatically detecting Japanese palindromes, you must explicitly define which rule set to adopt.

Anagrams - The Combinatorial Explosion of Letter Rearrangement

An anagram rearranges the letters of a word or phrase to form a different meaningful word or phrase. Famous English examples include "listen" and "silent," and "astronomer" and "moon starer." Japanese has such pairs too, such as "tokei" (clock) and "keito" (yarn), where rewriting the word into hiragana and rearranging it yields another word.

The theoretical number of anagram combinations from an n-character word is n! (n factorial). However, duplicates must be removed when the same character appears multiple times.

Character CountAll Unique Characters (n!)ExampleBrute-force time at 1 million tries per second
3 characters6 combinationscat → actInstant
6 characters720 combinationslisten → silent, enlist, tinselInstant
7 characters5,040 combinationsthicken → kitchenInstant
10 characters3,628,800 combinationsastronomer → moon starerAbout 3.6 seconds
15 charactersAbout 1.3 trillion-About 15 days

Take astronomer from the Example column: because "o" and "r" each appear twice, the number of genuinely distinguishable arrangements is 907,200, which is 10 factorial with the duplicates divided out. As the character count grows, the candidate count swells at factorial speed, but the number of entries in a dictionary does not grow in proportion. In other words, the longer the word, the thinner the density of hits, and the harder it becomes to land on a single other word. That is why anagrams of long words usually take the form of a multi-word phrase, as in astronomer → moon starer.

The most efficient way to detect anagrams programmatically is to sort the characters of both strings and compare them. Sorting "listen" yields "eilnst," and sorting "silent" also yields "eilnst," confirming they are anagrams. This method has O(n log n) time complexity. An even faster O(n) algorithm counts the frequency of each character and compares the counts.

Anagrams have a deep connection to the history of cryptography. In the 17th century, scientists used anagrams to claim priority for their discoveries. Galileo published his discovery of Saturn's rings as the anagram "smaismrmilmepoetaleumibunenugttauiras," later revealing it to be an anagram of "Altissimum planetam tergeminum observavi" (I have observed the highest planet to be triple).

Pangrams - The Challenge of Using Every Letter

A pangram is a sentence that uses every letter of the alphabet at least once. The most famous English pangram is "The quick brown fox jumps over the lazy dog," which contains all 26 letters in 35 characters (excluding spaces).

A perfect pangram uses each letter exactly once. An English perfect pangram must consist of exactly 26 characters, making it extremely difficult to form a meaningful sentence. "Mr Jock, TV quiz PhD, bags few lynx" (26 characters) is one somewhat forced example.

Japanese has what may be the world's most beautiful pangram: the Iroha poem.

"Iro ha nihoheto / chirinuru wo / waka yo tare so / tsune naramu / uwi no okuyama / kefu koete / asaki yume mishi / wehi mo sesu"

This poem uses all 47 kana characters of its era exactly once - a perfect pangram - while also being a meaningful waka poem expressing Buddhist impermanence. Its author is unknown, and even its date of composition can only be placed somewhere between the end of the 10th century and the middle of the 11th century, with the oldest surviving record appearing in a document from 1079. Recited for close to a thousand years without its author's name surviving, this poem is an intellectual heritage of humanity that achieves both mathematical constraint and literary beauty.

Modern Japanese kana consists of 46 characters ("wi" and "we" were abolished and "n" was added), but the Iroha poem doesn't contain "n." Many have attempted to create a perfect pangram with all 46 modern kana, but none have matched the beauty of the Iroha poem. The stricter the constraint, the greater the value of works that satisfy it.

English pangrams have long had practical uses in typing practice and in typeface samples. Among them, "The quick brown fox jumps over the lazy dog" is the sample sentence that circulates most widely. The reason is that it packs all 26 letters into 35 characters excluding spaces, letting you compare the shape of every letter in a typeface at a glance. Note, though, that the only capital letter it produces is the leading T; the shapes of the remaining capitals, and of digits and symbols, cannot be checked from it. When actually selecting a typeface, this sentence has to be paired with a sample that includes numerals and punctuation. Here too, how compact the sample is and how much it lets you verify are a trade-off.

Pangram TypeLanguageCharacter CountCharacter Set Used
The quick brown fox...English35 charactersa-z (26 letters, with repeats)
Mr Jock, TV quiz PhD...English26 charactersa-z (perfect pangram)
Iroha poemJapanese47 characters47 kana (perfect pangram)
Portez ce vieux whisky...French37 charactersa-z (26 letters, with repeats)

Lipograms - The Constraint of Avoiding Specific Letters

A lipogram is the opposite of a pangram - writing text without using a specific letter at all. The most famous lipogram work is the novel "La Disparition" (A Void) published by French author Georges Perec in 1969. This approximately 300-page novel was written entirely without using "e," the most frequently used letter in French.

In English, Ernest Vincent Wright's "Gadsby," published in 1939, is well known. The entire novel of approximately 50,000 words avoids the letter "e." Since "e" appears in about 12.7% of English text - the most frequently used letter - writing a full-length novel while excluding it demands extraordinary linguistic ability.

In the Unicode world discussed in emoji character counting, with over 140,000 available characters, avoiding a specific character is trivial. However, writing meaningful text while excluding a specific letter from a natural language's limited character set is an intellectual challenge on a different dimension from character count constraints.

Ambigrams - Text Designs Readable When Rotated

An ambigram is a design where text can be read (as the same or different word) when rotated 180 degrees or reflected in a mirror. They became widely known through Dan Brown's novel "Angels & Demons."

Ambigrams are not purely a character count problem but rather designs that exploit the visual symmetry of letters. Among English uppercase letters, "A," "H," "I," "M," "O," "T," "U," "V," "W," "X," and "Y" are horizontally symmetric, while "H," "I," "N," "O," "S," "X," and "Z" still look like the same shape after a 180-degree rotation. The point that is easy to miss here is that a word readable after rotation has to satisfy two conditions at once: every letter has to return to its own original shape, and the reversed order of the letters has to still spell the word. "NOON" and "SOS" are written with rotationally symmetric letters and are palindromes in themselves, so they meet both conditions with no design work added.

"SWIMS" goes one step further. Neither W nor M returns to its own shape when rotated 180 degrees on its own, but each turns into the other's shape. Reading S, W, I, M, S back in reverse still gives S, W, I, M, S, so the word can be read as rotationally symmetric as well. Looking not only for individually symmetric letters but also for pairs of letters that swap into each other under rotation is the real crux of ambigram design.

The designer who opened up this form of expression was the graphic designer John Langdon (1946-2026). In 1972 he drew "heaven" as a word that could also be read upside down, and he stands alongside Scott Kim, who arrived at the same idea independently, as a pioneer of the form. The name "ambigram" itself was coined by Douglas Hofstadter in 1984. Langdon created the ambigrams for Dan Brown's novel "Angels & Demons," and the novel's protagonist, Robert Langdon, is said to have been named partly after him. Fewer characters make ambigram design easier, but creating ambigrams of long words or sentences is an artistic challenge requiring advanced design skills.

Numeric Palindromes and Mathematics - 196 and Lychrel Numbers

Palindromes exist in the world of numbers too. Palindromic numbers like 121, 1331, and 12321 have mathematically interesting properties. For any natural number, repeating the operation of "adding the number with its digits reversed" often leads to a palindromic number. For example: 59 → 59 + 95 = 154 → 154 + 451 = 605 → 605 + 506 = 1111 (palindromic number).

However, when this operation is performed on the number 196, no palindromic number is reached even after millions of iterations. A number conjectured never to become a palindrome no matter how many times the operation is repeated is called a Lychrel number, and 196 is the smallest candidate in base 10. The search has a long history: in 1990 about 2.42 million iterations reached one million digits, in 2006 the count reached 300 million digits, and in 2011 one billion iterations reached a number of 413.93 million digits, yet no palindrome has appeared (as of 2026). What has to be noted here is that the iteration count and the digit count are two different things. A single addition adds at most one digit, so "one billion additions were computed" does not mean "one billion digits were reached." And however far the digit count is extended, that only amounts to the fact that no counterexample has been found yet; it does not prove that 196 really is a Lychrel number. Counting characters (digits) alone cannot settle this question, and that is exactly where the mathematical depth behind the simple concept of a palindrome shows.

Semagrams - Messages Hidden in Non-textual Elements

As a type of wordplay, semagrams are also worth mentioning. A semagram hides messages not in the characters themselves but in the decoration or layout of characters. For example, making specific letters slightly bolder in a letter, or subtly varying the spacing between certain words to convey a secret message.

In the digital world, research has been conducted on embedding bit information by manipulating font kerning (character spacing). Normal kerning represents "0" and slightly wider kerning represents "1," embedding binary data throughout a document. Since this technique hides messages without changing the character count at all, it cannot be detected by character counting.

Programming and Wordplay - Practical Applications of Character Constraints

These wordplay forms are not merely intellectual entertainment. Palindrome detection is fundamental to string algorithms and is applied to DNA sequence analysis (palindromic sequences are restriction enzyme recognition sites). Anagram detection relates to hash function design, and pangrams are used for font preview displays (this is exactly why "The quick brown fox..." appears in font samples).

WordplayCharacter ConstraintComplexityPractical Application
PalindromeFront-back symmetryDetection: O(n)DNA sequence analysis, data validation
AnagramRearrangement of same charactersDetection: O(n log n)Cryptography, hash functions
PangramContains all charactersDetection: O(n)Font preview, typing practice
LipogramExcludes specific charactersDetection: O(n)Stylistic analysis, author attribution
AmbigramVisual symmetry-Logo design, cryptography

Counting the length of a string with a character counting tool is the starting point for all these wordplay forms. Whether a palindrome's character count is even or odd changes how the center character is handled, increasing anagram character count causes combinatorial explosion, and the character set size becomes the constraint for pangrams. Beyond the simple act of counting characters lies the vast world of combinatorics and computational complexity theory.

Share this article