Menu
CoddyTech

Longest Palindromic Substring

You get a string s of lowercase English letters. Return its longest palindromic substring: the longest run of consecutive letters that reads the same forward and backward. If several substrings share that longest length, return the one that starts furthest to the left.

Function

longestPalindrome(s: string) → string
sstring
the lowercase string to search
Returnsstring
the longest palindromic substring of s, the leftmost one when several tie

Constraints

  • 1 ≤ s.length ≤ 2000
  • s holds only lowercase English letters.
  • When several palindromes have the longest length, the answer is the one with the smallest start index.

Examples

Input
s = "bananas"
Output
"anana"
Explanation
"anana" reads the same from both ends and has 5 letters. No longer piece works: "banana" starts with b and ends with a, "ananas" starts with a and ends with s, and the whole word starts with b and ends with s.

lock icon+18 hidden tests on Submit

challenge icon

Follow-up

Can you find the answer in O(n) time?

Reset code
def longestPalindrome(s):
    # Write code here
Test cases

Case 1

Case 2

Case 3

Input

s = "bananas"

Expected

"anana"