Published by Dimitris Kougioumtzis · Updated
Expand around odd and even centers; ties choose the earliest starting position.
Fill intervals by increasing length; matching endpoints form a palindrome only when the inner interval does too.
When to use Longest palindromic substring
Finding a contiguous mirrored region.
Choosing an alternative
Palindromic subsequence may skip characters; Manacher offers a stronger substring bound.
Worked example: input and state changes
Example input
babad
These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.
Step 0: Initial state.
State at this checkpoint
{ "values": [ "b", "a", "b", "a", "d" ], "active": [] }Step 2: Expand matching center: interval [1,1].
State at this checkpoint
{ "values": [ "b", "a", "b", "a", "d" ], "active": [ 1, 1 ] }Step 4: Expand matching center: interval [2,2].
State at this checkpoint
{ "values": [ "b", "a", "b", "a", "d" ], "active": [ 2, 2 ] }Step 6: Expand matching center: interval [3,3].
State at this checkpoint
{ "values": [ "b", "a", "b", "a", "d" ], "active": [ 3, 3 ] }Step 8: Longest palindromic substring: bab; length = 3.
State at this checkpoint
{ "values": [ "b", "a", "b", "a", "d" ], "active": [ 0, 1, 2 ], "substring": "bab", "start": 0, "length": 3 }
Expected final state
Longest palindromic substring: bab; length = 3.
Complexity and input bounds
Family: Strings. Time complexity: O(n²).
Space complexity: O(n squared) interval table.
Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.
One string of 1–24 ASCII letters.
The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.
Common mistakes and limitations
A substring must be contiguous; a longer noncontiguous answer solves a different problem.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 | // a contains 1–24 ASCII letter codes. Returns longest contiguous palindrome length; output[0] receives its start index. Ties choose the earliest start. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation. function algorithm(a, n, output) { let best = 0; let start = 0; for (let center = 0; center < 2 * n - 1; center++) { let left = Math.floor(center / 2); let right = left + center % 2; while (left >= 0 && right < n && a[left] == a[right]) { if (right - left + 1 > best) { best = right - left + 1; start = left; } left--; right++; } } output[0] = start; return best; } |
Source
Primary research source for the algorithm or its underlying method. The playground is a bounded teaching implementation, not the publication's original program.
- Algorithms on Strings, Trees and Sequences
Dan Gusfield (1997). Reference book.