Longest Palindromic Substring

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

Write longestPalindrome(text): return the longest substring of text, a run of neighbouring characters, that reads the same backwards. Every character counts, and case matters. If several share the longest length, return the leftmost; return "" for an empty text. For example, longestPalindrome("babad") is "bab", which comes before "aba", and longestPalindrome("cbbd") is "bb". The checks include texts of 5,000 characters, so checking every substring is too slow: grow a palindrome outwards from each possible middle, a character or the gap between two.

Your code must pass

  • Finds the longest palindrome in the examples
  • Finds palindromes of even length
  • Returns the leftmost of equal-length palindromes
  • Handles one character and an empty text
  • Is fast enough for 5,000 characters