Back to problems Sign in to unlock
Leetcode
Medium
Strings
Dynamic Programming
Recursion
Google
Meta
Longest Palindromic Substring
Find the longest contiguous substring of a given string that reads the same forward and backward.
Acceptance 100%
Problem Statement
Given a string s, return the longest contiguous substring that is a palindrome.
A palindrome is a string that reads the same from left to right and from right to left. If there are multiple answers with the same maximum length, any one of them is acceptable.
Input Format
- A single string
s. - The string may contain lowercase/uppercase letters and other visible characters depending on the platform variant.
Output Format
- Return the longest palindromic substring of
s.
Constraints
1 <= |s|- The exact upper bound is platform-dependent; assume the string length can be large enough that an solution is acceptable for interview discussion.
- The answer must be contiguous.
Examples
Sample cases returned by the problem API.
Example 1
Input
s = "babad"
Output
"bab"
Explanation
"bab" and "aba" are both valid longest palindromic substrings. Either one is acceptable.
Example 2
Input
s = "cbbd"
Output
"bb"
Explanation
The longest palindromic substring is "bb".
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.