Problem · String
Longest Palindromic Substring
Learn this problemProblem statement
Given a string s, return its longest contiguous substring that reads the same from left to right and from right to left.
If more than one longest palindromic substring exists, return the one whose first character appears earliest in s.
Function
longestPalindrome(s: String) → StringExamples
Example 1
s = "babad"return = "bab"Both bab and aba have maximum length three. The earlier one is bab.
Example 2
s = "cbbd"return = "bb"The two middle characters form the unique longest palindrome.
Example 3
s = "forgeeksskeegfor"return = "geeksskeeg"The ten-character middle substring is palindromic and no longer substring is.
Constraints
1 <= s.length <= 2000.scontains only uppercase English letters, lowercase English letters, and digits.- Character comparisons are case-sensitive.