Longest Palindromic Substring
Given a string s, return the longest palindromic substring in s.
A palindrome is a string that reads the same forward and backward.
A substring is a contiguous sequence of characters within a string.
If there are multiple answers with the same maximum length, return any one of them.
Example 1
Input
s = "babad"
Output
"bab"
Explanation
"bab" is a palindrome because it reads the same forward and backward:
bab → bab
"aba" is also a valid longest palindromic substring, so "aba" would also be accepted.
Example 2
Input
s = "cbbd"
Output
"bb"
Explanation
The substring "bb" is a palindrome:
bb → bb
It is the longest palindromic substring in "cbbd".
Constraints
Hints:
Hint 1
Every palindrome has a center.
The center can be:
- One character → odd-length palindrome
- Two characters → even-length palindrome
Hint 2
Try expanding outward from every possible center.
For example:
b a b
↑
center
Expand while the characters on both sides are equal.
Author & Technical Reviewer
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output