Blind 75
52 / 75Free practice

Longest Palindromic Substring

Medium · Dynamic programming

Jump to code

Return a longest contiguous substring that reads the same forward and backward. If several have the same maximum length, any is acceptable.

Examples

Example 1

s = "babad"

Output: "bab"

Example 2

s = "cbbd"

Output: "bb"

Constraints

  • 1 <= s.length <= 1000
  • s contains only digits and English letters.
Need a hint?

A palindrome grows symmetrically around a character or a gap.

Asked in real interviews

Candidates reported versions of Longest Palindromic Substring from these company interviews. Some company questions are locked and open with Premium or XP.

Browse all real coding interview questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 7 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  "babad"
]
Expected
bab
Case 2
Input
[
  "cbbd"
]
Expected
bb
Case 3
Input
[
  "a"
]
Expected
a
Case 4
Input
[
  "racecar"
]
Expected
racecar
Case 5
Input
[
  "forgeeksskeegfor"
]
Expected
geeksskeeg

Drafts are saved per language in this browser when storage is available.