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 both ways and has 5 letters. Every longer piece fails at its ends: "banana" (b, a), "ananas" (a, s) and "bananas" (b, s).
challenge icon

Follow-up

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

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

Test #1

Test #2

Test #3

Argument

s = "bananas"

Expected Output

"anana"