Technique
Manacher's Algorithm
Compute the palindrome radius at every centre in O(n) by mirroring radii inside the rightmost known palindrome.
0%
mastery
0/6 solved
0/6 solved
Recognise it
The signal
longest palindromic substring with n up to 1e6
1Brute force
Expand around each centre O(n2).
2The observation
Re-expanding: inside a known palindrome, the mirror's radius is a lower bound.
3Why it works
[l, r] is the rightmost palindrome found so far; d[i] >= min(d[mirror], r - i).
Reach for it when
- +count all palindromic substrings in O(n)
Not when
- ×palindromic SUBSEQUENCE (dp-palindrome)
- ×palindrome queries on substrings with updates (hashing)
Practice
0/6 solved
- Prefix-Suffix Palindrome (Hard version)Codeforces1800
- Longest Palindromic SubstringAmazon, MicrosoftMedium
- Palindromic SubstringsMeta, AdobeMedium
- Longest PalindromeCSESMedium
- All PalindromesCSESHard
- Shortest PalindromeHard
Pitfalls
Misconceptions
- !odd/even centres (use the '#' interleave or two arrays)