跪拜 Guibai
← All articles
JavaScript · Frontend · Interview

Four Ways to Find the Longest Palindrome, from O(n³) to O(n)

By 胡萝卜术 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

The center-expansion method is the practical takeaway for any developer who might face a string-manipulation interview; it is fast enough, uses no extra memory, and is short enough to write on a whiteboard. Manacher's, while rarely required, is the canonical example of how symmetry can drive an algorithm to its theoretical limit.

Summary

A brute-force enumeration of all substrings runs in O(n³) and times out on LeetCode. Dynamic programming cuts the time to O(n²) by caching whether smaller substrings are palindromic, at the cost of O(n²) memory. Center expansion achieves the same O(n²) time with O(1) space by growing outward from each possible palindrome center — the approach most recommended for interviews. Manacher's Algorithm unifies odd- and even-length palindromes with a preprocessing step and exploits symmetry to reach O(n) time, though the code is harder to produce under pressure. The progression from brute force to Manacher's mirrors a classic optimization arc: identify redundant work, cache intermediate results, then exploit structural properties to eliminate the cache.

Takeaways
Brute force checks every substring for palindrome property, yielding O(n³) time and immediate timeout on LeetCode.
Dynamic programming fills an n×n boolean table using the rule dp[i][j] = dp[i+1][j-1] when s[i] == s[j], running in O(n²) time and O(n²) space.
Center expansion treats each character and each gap between characters as a palindrome center, expanding outward in O(n) per center for O(n²) total time and O(1) space.
Manacher's Algorithm inserts # separators to make every palindrome odd-length, then reuses previously computed radii to skip redundant expansions, achieving O(n) time.
Interviewers most often expect the center-expansion solution; Manacher's is a bonus demonstration of algorithmic depth.
Conclusions

The four-solution progression is a miniature curriculum in algorithm optimization: identify repeated work, cache it, then find a structural insight that makes the cache unnecessary.

Center expansion is often superior to dynamic programming for palindrome problems because the symmetry property lets you discard the DP table entirely without losing time efficiency.

Manacher's Algorithm is less about raw speed in practice — O(n²) is fine for typical constraint sizes — and more about proving that a linear-time solution exists for a problem that initially looks quadratic.

Concepts & terms
Palindrome
A string that reads the same forward and backward, such as "aba" or "abba". Palindromes are centrally symmetric.
Center Expansion
A technique that picks each character (and each gap between characters) as a potential palindrome center, then expands outward while the characters on both sides match. It finds the longest palindrome in O(n²) time and O(1) space.
Manacher's Algorithm
A linear-time algorithm for the longest palindromic substring. It first inserts a separator character (e.g., #) between every original character to unify odd- and even-length palindromes, then uses a cached radius array and the current rightmost palindrome boundary to skip redundant expansions.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗