5. Longest Palindromic Substring¶
Edited by Charles
Medium · Python · Dynamic Programming
Problem
Read the original problem on LeetCode.
The official page contains the problem statement, examples, and constraints.
Explanation, examples & other approaches
Core idea
Given a string s, return the longest palindromic substring in s.
A palindrome is a string that reads the same forward and backward.
Original study notes follow, including intermediate snippets and earlier attempts.
Problem¶
Given a string s, return the longest palindromic substring in s.
A palindrome is a string that reads the same forward and backward.
For example:
Important:
This problem asks for a substring, so all characters must be continuous.
"aba" is also a valid answer.
Another example:
The key question is:
How can we determine whether
s[i:j+1]is a palindrome?
Consider:
For this substring to be a palindrome, two conditions must hold:
This naturally leads to Dynamic Programming.
Define:
as:
Whether the substring
s[i:j+1]is a palindrome.
Therefore:
means:
is a palindrome.
For example:
If:
then:
so:
Suppose we want to determine:
For it to be a palindrome, first:
must be true.
Then we need to check whether the inside substring is also a palindrome:
which corresponds to:
Therefore, for a normal substring:
There is an important boundary case.
Length = 1¶
A single character is always a palindrome:
Length = 2¶
For example:
There is no inner substring that needs to be checked.
As long as:
the substring is a palindrome.
So we use:
to handle substrings of length 1 or 2.
Therefore, the complete transition is:
Equivalently:
This is one of the most important parts of this DP.
Our transition is:
depends on:
So before calculating:
we must already know:
Therefore, i must iterate from large to small:
and j moves forward:
Imagine the DP table:
To calculate:
we need:
which is located one row below and one column to the left.
Therefore:
so i goes backward.
We define:
for substring:
A valid substring requires:
Therefore:
We only calculate the upper-right half of the DP table.
There is no need to calculate:
because that does not represent a valid substring.
Whenever:
we have found a palindrome:
Its length is:
So if this palindrome is longer than the current maximum:
we update:
At the end, the longest palindrome starts at:
and has length:
Therefore:
Consider:
Indices:
Single Characters¶
Every single character is a palindrome:
Therefore:
Check "aba"¶
Consider:
Substring:
First:
Then check the inside:
which represents:
A single character is a palindrome:
Therefore:
So:
is a palindrome.
Check "bab"¶
Consider:
Substring:
We have:
and:
Therefore:
So "bab" is also a palindrome.
The longest length is:
A common mistake is to write something like:
This type of transition allows us to skip characters.
For example:
means:
That is appropriate for a subsequence problem.
But LeetCode 5 asks for:
A substring must be continuous.
Therefore, we cannot skip characters.
This is the key difference between:
and:
Time Complexity¶
There are approximately:
DP states.
Each state takes O(1) time.
Therefore:
Space Complexity¶
We use an n × n DP table:
The most important DP definition is:
The transition is:
Remember the logic:
And because:
the iteration order must ensure the inner substring is calculated first:
The main distinction to remember is:
Study record
| Field | Value |
|---|---|
| Attempts | 1 |
| Mastery | ❚ |
| The Latest Attempt | September 14, 2026 |
Python solution¶
class Solution:
def longestPalindrome(self, s: str) -> str:
n = len(s)
dp = [[False] * n for _ in range(n)]
start = 0
maxLength = 1
for i in range(n - 1, -1, -1):
for j in range(i, n):
if s[i] == s[j] and (j - i <= 1 or dp[i + 1][j - 1]):
dp[i][j] = True
if j - i + 1 > maxLength:
maxLength = j - i + 1
start = i
return s[start:start + maxLength]