← DSA Atlas
Dedicated problem page · #5

Longest Palindromic Substring

MediumOne-Dimensional Dynamic ProgrammingExpand around centerTwo-pointer expansion over palindrome centers
Solve on LeetCode ↗
05
MediumOne-Dimensional Dynamic ProgrammingTwo-pointer expansion over palindrome centersExpand around center

Longest Palindromic Substring

Given a string s, return the longest contiguous substring of s that reads the same forwards and backwards. If several have the maximum length, any one of them is accepted.

Open official problem prompt ↗
In plain English

Find the longest stretch of consecutive characters that is a mirror image of itself.

Picture it like this

Standing at each seam of a row of tiles and pushing both hands outward as long as the tiles on the left and right match -- the widest symmetric span you reach is the answer.

Example
Input
s = "babad"
Output
"bab"
Why
"bab" is a palindrome of length 3; "aba" is an equally valid alternative.
Constraints
1 <= s.length <= 1000s consists of digits and English letters
Pattern lesson

See the pattern, then code

Expand around center
Recognition clue

Asking for the longest palindromic substring (contiguous, symmetric) suggests growing outward from each possible center.

One-Dimensional Dynamic Programming

Count ways or optimize a result where each state depends on earlier positions.. Every palindrome has a center; expanding two pointers outward while characters match finds the longest palindrome anchored at that center.

New words, made simpleKnow these before the algorithm
Palindrome
A string equal to its own reverse.
Center
A single character (odd length) or a gap between two characters (even length) around which a palindrome is symmetric.
Substring
A contiguous slice of the string.
Approaches from first idea to best ideaCompare the trade-offs
ApproachHow it thinksCost
Check every substring

The palindrome test adds another factor of n; too slow.

For all O(n^2) substrings test whether each is a palindrome.

Time O(n^3)Space O(1)
The rule we keep true

Invariant

After processing center i, (start, end) records the longest palindrome found among all centers up to and including i.

Why this is correct

Reasoning

Every palindrome is uniquely determined by its center and expands symmetrically; there are exactly 2n-1 centers (n single characters and n-1 gaps). Checking maximal expansion at each center therefore examines every palindrome's longest form, so the global maximum is found.

The algorithm in three movesSay these aloud before coding
1Iterate every index as a potential center

center i=1 odd: expand 'a' -> 'bab'

2Expand once treating i as an odd-length center and once treating i,i+1 as an even-length center

best span (0,2)

3Each expansion widens while both ends match and stay in bounds

even centers yield nothing longer

4Track the widest span seen and return that slice

Visual trace

Follow the data, one decision at a time

Input snapshotHighlighted cells are involved in this example
b0
a1
b2
a3
d4
1 · Readodd center 0
2 · Askexpand while equal?
3 · Update statespan (0,0)
4 · Resultleft goes out of bounds immediately; length 1.
Key takeaway

Expanding around index 1 grows outward to the palindrome 'bab'.

Code walkthrough

Read the solution in small chunks

Python 3

Do not memorize the whole program. Connect each group of lines to one job in the algorithm.

  1. 1
    Lines 3-5Guard and best span

    Handle empty input and remember the best (start, end) window found so far.

  2. 2
    Lines 6-10Expansion helper

    Widen l and r while in bounds and matching, then return the last valid inclusive span (l+1, r-1).

  3. 3
    Lines 11-17Try both center types

    For every index run one odd expansion and one even expansion, updating the best window when a wider palindrome appears.

Make it stick

Edge cases, mistakes, and one self-check

E

Edge cases to test

  • Single character returns that character
  • All identical characters returns the whole string
  • No palindrome longer than 1 (e.g. 'abc') returns any single character
  • Even-length palindrome like 'cbbd' must be caught by the even center
!

Common beginner mistakes

  • Forgetting even-length centers and missing palindromes like 'abba'
  • Off-by-one when converting the expansion's final l,r back to the valid inclusive span (l+1, r-1)
  • Comparing lengths with r-l+1 inconsistently versus r-l when updating the best
Check your understanding

Why must you expand around both a single index and a pair of adjacent indices?