Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Dynamic Programming/Palindromic Substrings

Technique

Manacher's Algorithm

Compute the palindrome radius at every centre in O(n) by mirroring radii inside the rightmost known palindrome.

Learn firstString Matching (KMP / Z / Rabin-Karp)Palindromic Substrings
0%
mastery
0/6 solved
Practise nextPrefix-Suffix Palindrome (Hard version)
IdeaPracticePitfalls

Recognise it

The signal

longest palindromic substring with n up to 1e6

  1. 1Brute force

    Expand around each centre O(n2).

  2. 2The observation

    Re-expanding: inside a known palindrome, the mirror's radius is a lower bound.

  3. 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

Next

Prefix-Suffix Palindrome (Hard version)

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)
Go deeperresources

Resources

  • DP lecture notesErrichto · video
  • AtCoder Educational DP ContestAtCoder · sheet
  • Introduction to DPUSACO Guide · course
  • Dynamic Programming PatternsLeetCode discuss · article
  • Dynamic Programming playlistAditya Verma · video
  • Bitmask DPUSACO Guide · course
  • Digit DPCodeforces · blog
TodayReviewPatternsProblems