Longest Palindromic Substring
Problem statement
Given a string s, return its longest palindromic substring.
A substring is a run of characters that sit next to each other in s. A palindrome reads the same from left to right and from right to left. A single character is a palindrome.
Compare characters exactly, so an uppercase letter and its lowercase letter are different. If several longest palindromic substrings exist, return the one that starts first in s.
Function
longestPalindrome(s: String) → StringExamples
Example 1
s = "abacdc"return = "aba"The palindromes of length 3 are "aba", which starts at index 0, and "cdc", which starts at index 3. No palindrome is longer.
They tie, so return the one that starts first: "aba".
Example 2
s = "noonday"return = "noon""noon" reads the same both ways and has length 4. No longer substring does, so the answer is "noon".
Example 3
s = "Aa"return = "A""A" and "a" are different characters, so "Aa" is not a palindrome.
Both one-letter substrings are palindromes of length 1. "A" starts first, so it is the answer.
Constraints
1 <= s.length <= 1000scontains only English letters (uppercase and lowercase) and digits.