Editor

Longest Palindromic Substring

#137 · Strings · Advanced · 125 XP · about 25 min

A palindrome reads the same forwards and backwards, like "level". Write longest_palindrome(s) that returns the longest unbroken piece of s that is a palindrome. If several pieces share the longest length, return the one that starts furthest to the left.

The comparison is exact: uppercase and lowercase letters are different, and spaces count as characters.

Examples

longest_palindrome("bananas")    # returns 'anana'
longest_palindrome("noonmadam")  # returns 'madam'
longest_palindrome("abcd")       # returns 'a'

In "abcd" every single letter is a palindrome of length 1, and "a" starts first.

Constraints

  • s has 0 to 2,000 characters.

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.