Loading content...
A symmetric string (also known as a palindrome) is a sequence of characters that reads identically from left to right and from right to left. For example, "radar", "level", and "noon" are all symmetric strings, while "hello" is not.
Given a string s consisting of alphanumeric characters, your task is to identify and return the longest contiguous substring within s that is symmetric. A substring is a contiguous sequence of characters within the original string.
If multiple symmetric substrings of the same maximum length exist, you may return any one of them as your answer. The input is guaranteed to contain at least one character, and every single character is trivially symmetric.
s = "babad""bab"The symmetric substrings within "babad" include "b", "a", "bab", and "aba". Both "bab" and "aba" have length 3, which is the maximum. Either answer is acceptable. Here we return "bab".
s = "cbbd""bb"The longest symmetric substring is "bb" with length 2. Single characters like "c", "b", "d" are also symmetric but shorter.
s = "racecar""racecar"The entire string "racecar" is symmetric because it reads the same forwards and backwards. This is the longest possible symmetric substring.
Constraints