Skip to main content

Longest Palindromic Substring

Given a string s, return the longest palindromic substring in s.

Problems13 / 152
Sign in

Longest Palindromic Substring

MEDIUMStringDynamic Programming

Given a string s, return the longest palindromic substring in s.

Example 1

Input:  s = "babad"
Output: "bab"

Example 2

Input:  s = "cbbd"
Output: "bb"
Show hint

For each character, expand around it to check for palindromes (odd and even length). Keep track of the longest palindrome found.

Draft saves automatically

Loading editor...

Submit also runs hidden tests
s =
"babad"
Expected
"bab"
Run with your own input (JSON)