↑ ↓ to move · enter to open

009 · Easy · Strings, two pointers

palindromes

A palindrome reads the same forwards and backwards. Checking one takes a single pass with two pointers that walk inward from both ends. The same mirror idea finds the longest palindrome hidden inside a string, and it drives a number puzzle that is still open for 196.

LAB 1

two pointers meet in the middle

First clean the phrase: keep only letters and digits, and lowercase them. Then put pointer L on the first character and R on the last. Compare, step both inward, repeat. One mismatch ends the search. A string of length n needs at most ⌊n/2⌋ comparisons.

Raw input
↓ keep letters and digits, lowercase
Normalized
Try

How to read this: L and R start at both ends and step toward the middle, comparing one pair at a time. The small numbers under the tiles are positions, counting from 0.

  • pair being compared
  • pair already matched
  • mismatch, so not a palindrome
  • every pair matched
  • struck-out tile: removed by cleaning
—
Verdict
—
Length n after cleaning
—
Comparisons / max ⌊n/2⌋
—
Characters dropped

This step—

The codeO(n) time · O(1) extra space


        

Why it works

A string is a palindrome exactly when character k equals character n−1−k for every k. The pointers visit those mirrored pairs once each, outermost first. When they meet, every pair has been checked; a middle character in an odd-length string has no partner and never needs one.

Switch to Raw string and the Panama phrase fails on its very first comparison, 'A' against 'a'. Cleaning is part of the definition you choose.

LAB 2

expand around every center

Every palindrome has a center: a letter for odd lengths, or the gap between two letters for even lengths. A string of n letters has 2n−1 centers. From each one, grow outward while the two new ends match. The longest run found is the answer, marked in gold.

▼ current center · numbers show the palindrome length found at each center
Try

How to read this: every letter and every gap between letters (the narrow columns) is a possible middle. From the ▼, the search grows outward while both ends match.

  • palindrome found so far from this center
  • the two letters being compared
  • mismatch, stop
  • longest so far

Try this: aaaa… to see the slowest case.

—
Longest so far
—
Centers done / 2n−1
—
Comparisons, expand
—
Comparisons, check every substring

This step—

The codeO(n²) time · O(1) space


        

Why centers beat substrings

There are about n²/2 substrings, and checking each one with two pointers can cost up to n/2 more steps, so the brute force is O(n³). Expanding from a center reuses work: once "ee" is known to be a palindrome, "keek" needs just one more comparison.

Each center stops at its first mismatch, so a string with no repeated neighbors finishes in about 2n comparisons. A run of one letter such as aaaa… is the worst case, about n²/2. Manacher's algorithm reuses mirror information across centers to reach O(n).

LAB 3

reverse and add

Take a number, add it to its own digit reversal, and repeat until the sum is a palindrome. Most numbers get there fast: 87 takes 4 steps. 89 takes 24 steps and climbs to thirteen digits. For 196 nobody has ever found a palindrome, and nobody has proven that none exists.

  • latest step
  • palindrome reached
  • gave up at the cap
Every start from 0 to 999, 40 per row, 0 at top-left · click one to load it
already a palindromefew stepsmany stepsno palindrome within the caploaded
Hover a cell.
Try
—
Steps
—
Digits now
—
Status
—
Unresolved below 1,000

This step—

The codeBigInt, capped


        

The 196 problem

A number that never reaches a palindrome this way is called a Lychrel number. No base-10 number has been proven to be one. 196 is the smallest candidate: computers have run its sequence to enormous lengths, and the sum keeps growing without ever reading the same both ways. Load it and watch the digit count climb.

Scanning 0 to 999…

This page stops after 500 steps. That is a limit of patience, not a proof. The sums here can run to hundreds of digits, so the arithmetic uses JavaScript's BigInt to stay exact.