Longest Palindromic Substring - Problem
Given a string s, return the longest palindromic substring in s.
A palindrome is a string that reads the same forward and backward. For example, "racecar" and "aba" are palindromes.
If there are multiple palindromic substrings of the same maximum length, return any one of them.
Input & Output
Example 1 — Basic Case
$
Input:
s = "babad"
›
Output:
"bab"
💡 Note:
Both "bab" and "aba" are valid palindromes of length 3. Either can be returned as they are both the longest.
Example 2 — Even Length Palindrome
$
Input:
s = "cbbd"
›
Output:
"bb"
💡 Note:
The longest palindromic substring is "bb" with length 2. Single characters are also palindromes but shorter.
Example 3 — Single Character
$
Input:
s = "a"
›
Output:
"a"
💡 Note:
A single character is always a palindrome, so return the entire string.
Constraints
- 1 ≤ s.length ≤ 1000
- s consist of only digits and English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input
String with characters to analyze
2
Process
Find all palindromic substrings and identify the longest
3
Output
Return the longest palindromic substring
Key Takeaway
🎯 Key Insight: Every palindrome has a center - expand outward from each possible center position to find all palindromes efficiently
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code