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

O(n²)

Expand around centers, or 2D dp where dp[i][j] depends on dp[i+1][j−1] plus matching ends.

  1. 1Learnthe lesson(you are here)
  2. 2Guidedwatch it run, line by line(not yet)
  3. 3Practisethe path, or the climb(not yet)
  4. 4Revisereviews on schedule(not yet)
Start the lesson
Learn firstInterval DP
0%
mastery
0/7 solved
IdeaPracticeTemplatePitfalls

Recognise it

The signal

Longest palindromic substring/subsequence, count palindromic substrings, or min cuts to split into palindromes.

  1. 1Brute force

    Check every substring O(n3).

  2. 2The observation

    Rechecking substrings: inner palindromes are reused.

  3. 3Why it works

    When pal[i][j] is computed, every shorter span inside it (i+1..j-1) is already correct.

Reach for it when

  • +minimum cuts into palindromes

Not when

  • ×n up to ~1e6, O(n²) too slowuse Manacher, O(n)
  • ×Palindrome queries with updates to the stringuse Rolling hash + segment tree

Practice

Next · Foundation

Palindromic Substrings

Expand around 2n-1 centres.

0/7 solved
  • Palindromic SubstringsFoundationFoundationMeta, AdobeMedium
  • Longest Palindromic SubstringApply it · Longest one.Apply itAmazon, MicrosoftMedium
  • Longest Palindromic SubsequenceOne change · Subsequence, not substring.One changeMedium
  • Minimum Insertion Steps to Make a String PalindromeTwists · Disguised subsequence problem.TwistsHard
  • Palindrome Partitioning IICombine · Partition DP on top of the pal table.CombineHard
  • Longest PalindromeMastery checkMastery checkCSESMedium
  • All PalindromesCSESHard

Pitfalls

Classic bugs

  • !Mixing substring (contiguous, boolean table) with subsequence (can skip chars, length table): different recurrences.
  • !Wrong fill order: iterate by increasing length, or i from n-1 down, so dp[i+1][j-1] is ready.
Go deepervariants and techniques · 1 combinations · resources

Variants and techniques

  • Manacher's AlgorithmCompute the palindrome radius at every centre in O(n) by mirroring radii inside the rightmost known palindrome.

Combinations

  • Palindromic Substrings+1D / Fibonacci-style

    minimum palindrome partition cuts

    Palindromic Substrings:
    O(1) palindrome check for any substring
    1D / Fibonacci-style:
    min cuts over prefixes

    Palindrome Partitioning II

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
Longest Increasing SubsequenceGrid / Matrix DP
TodayReviewPatternsProblems

Template · clean C++, Java and Python to revise from

Start the free 7-day trial