Top 150 InterviewMedium

Longest Palindromic Substring

Detailed guide and Python implementation for the 'Longest Palindromic Substring' problem.

Problem Statement

Medium

Given a string s, return the longest palindromic substring in s.

Write a function longestPalindrome(s: str) -> str.

Constraints
  • 1 <= len(s) <= 1000
  • s consists of only digits and English letters

Examples

Example 1
Input
s = "babad"
Output
"bab"
Explanation

"aba" is also a valid answer.

Example 2
Input
s = "cbbd"
Output
"bb"
Explanation

The longest palindromic substring is "bb".

Need a Hint?
Consider using 1D DP-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.