VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 12: Dynamic programming

Lesson 12.13 of 16 in this module Unrolled Pro

Longest Palindromic Substring

The longest contiguous substring that reads the same forwards and backwards. From checking every substring (O(n³)), to an interval DP where s[i..j] is a palindrome if its ends match and s[i+1..j−1] is one (O(n²) time and space), to expanding around each of the 2n − 1 centres (O(n²) time, O(1) space), and Manacher's algorithm, which reuses mirror information for O(n). Then counting palindromic substrings, the longest palindromic subsequence, and detecting repeated or mirrored spans in generated text.

about 10 min 3 Python examples 2 quick checks

Problem. Given a string s, return its longest contiguous substring that is a palindrome.

text
s = "babad"   →   "bab"   (or "aba")
s = "cbbd"    →   "bb"

This is an interval DP: the state is a range [i, j], and a range is a palindrome when its two ends match and the range strictly inside is a palindrome. That inside-out structure also suggests a simpler method: grow palindromes outward from their centres.

ML context. Symmetric and repeated spans matter in text and sequence data: detecting degenerate generations that mirror or repeat themselves, finding palindromic motifs in DNA (restriction sites, hairpin structures), and as a classic exercise in interval DP, the pattern behind parsing algorithms like CKY that build a parse tree over spans of a sentence.


1. Step 1: Clarify

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "Longest Palindromic Substring"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee