Problems

Longest Palindromic Substring

medium
medium
strings
dynamic-programming

Return the longest palindromic contiguous substring of s. If multiple answers have the same maximum length, return the one with the smallest starting index.

Input / output

  • Input: s: string
  • Output: longest palindrome

Examples

  1. "cbbd" returns "bb".
  2. "abaxyzzyxf" returns "xyzzyx".

Constraints

  • 1 <= s.length <= 1,000
  • Comparisons are case-sensitive.

Follow-up How do expand-around-center, dynamic programming, and Manacher's algorithm compare?

Examples

Example 1

Input: s = "racecar"
Output: "racecar"

Example 2

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

Example 3

Input: s = "forgeeksskeegfor"
Output: "geeksskeeg"
🔒 5 hidden

Running will execute all 8 cases, including 5 hidden ones.