Skip to content
Decoder

Longest Palindromic Substring

Medium

Problem

Return the longest substring of s that is a palindrome (reads the same forwards and backwards). A substring is a contiguous run of characters. Every test here has exactly one longest answer.

Example 1

Input
s = "cbbd"
Output
"bb"

Example 2

Input
s = "racecarxyz"
Output
"racecar"

Example 3

Input
s = "forgeeksskeegfor"
Output
"geeksskeeg"

Constraints

  • 1 <= s.length <= 1000
  • s contains only letters and digits.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.