Given a string, find the longest contiguous slice of it that is a palindrome, meaning it reads identically in both directions.
If several substrings share the maximum length, returning any one of them is acceptable. A single character is trivially a palindrome, so the answer is never empty when the input is non-empty.
Example 1
Input: s = "babad"
Output: "bab"
The slice "bab" is a palindrome of length three; "aba" would also be a valid answer.
Example 2
Input: s = "cbbd"
Output: "bb"
The longest mirror-image slice is "bb", since no palindrome of length three or more exists here.
Constraints
1 ≤ s.length ≤ 1000s consists of digits and English letters.See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI