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
Longest Palindromic Substring ProblemInput: "babad"babadPossible palindromes:• "b" (length 1)• "a" (length 1)• "bab" (length 3) ✓• "aba" (length 3) ✓• "d" (length 1)babOutput: "bab"🎯 Key Insight: Expand around each center to find palindromes efficiently
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
Asked in
Amazon 45 Microsoft 38 Google 32 Apple 25
125.0K Views
High Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen