Skip to content
BASA DSA

Longest Palindromic Substring

Medium1-D Dynamic Programming

Return a longest contiguous palindromic substring of s. Any maximum-length answer is valid; these fixtures have a unique maximum.

Example 1

Input
s = "cabbad"
Output
"abba"

Explanation

abba is an even palindrome that cannot extend through its surrounding c and d.

Example 2

Input
s = "racecarx"
Output
"racecar"

Explanation

racecar is an odd palindrome; trailing x cannot extend it.

Example 3

Input
s = "z"
Output
"z"

Explanation

A single character is its own longest palindrome.

Constraints

  • 1 <= len(s) <= 1000
  • s contains English letters and digits

Function contractlongestPalindromeArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…