Back to Practice
#0182

Longest Palindromic Substring

MediumDSA25 min20 XP

Problem

Given a string, return one longest palindromic substring.

Why This Matters

This problem teaches that two-pointer expansion can replace many repeated substring checks.

Function Signature

def longest_palindrome(s):

Examples

Example 1
Input"babad"
Output"bab" or "aba"

Both bab and aba are valid longest palindromes of length 3.

Constraints

  • Return the exact requested result.
  • Handle common edge cases.
  • Keep the solution readable before optimizing.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Input"babad"
Expected"bab" or "aba"

Both bab and aba are valid longest palindromes of length 3.

Hidden Test Categories
minimal inputrepeated valueslarger realistic input