Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-061

Longest Palindromic Substring

Problem

Return the longest palindromic substring of s; when several share the maximum length, return the first one found scanning centers left to right.

Expand around every center, treating both odd-length and even-length palindromes, and keep the longest expansion. Return the empty string for empty input.

Hint

Every palindrome has a center, either one character or a gap between two characters.

Test cases

case 1
in: ["babad"]
expected: "bab"
case 2
in: ["cbbd"]
expected: "bb"
case 3
in: [""]
expected: ""
case 4
in: ["a"]
expected: "a"
case 5
in: ["forgeeksskeegfor"]
expected: "geeksskeeg"
Starter codePython
def longest_palindrome(s):
    # Your code here
    pass

5 test cases · run your code against them in the editor