📘
Longest Palindromic Substring
MediumDynamic Programming
Given a string s, return the longest palindromic substring in s.
Example:
Input:
babad
Output:
bab
Reasoning:
- The input string
sisbabad, and we need to find the longest palindromic substring in it. - We check for palindromes centered at each character in the string, considering both odd-length and even-length palindromes.
- The longest palindromic substrings found are
babandaba, both with a length of 3. - Since both
babandabaare the longest palindromic substrings, the function can return either one, and in this case, it returnsbab.
Constraints:
- 1 <= len(s) <= 1000
- s consists of digits and English letters
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.