Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Longest Palindromic Substring

Medium Acceptance 34.8% Points 30

Given a string S, identify the longest palindromic substring within S. A palindromic substring is defined as a sequence of characters within S that reads the same forwards and backwards. For example, "racecar" and "level" are palindromic substrings.

Examples
Example 1

Example 1

Input: "abaxyzzyxf"

Output: "xyzzyx"

Explanation: The longest palindromic substring is "xyzzyx" of length 6.

Example 2

Example 2

Input: "bananas"

Output: "anana"

Explanation: The longest palindromic substring is "anana" of length 5.

Constraints
  • 1 <= |S| <= 10^4
Hints
  • Expected Time Complexity: O(n^2)
  • Expected Space Complexity: O(1)
Topics
String
Companies
Amazon Microsoft Samsung Visa Walmart Qualcomm Zoho
Added 19 Jul 2026 34.8% acceptance
Ln 1, Col 1Sign in to save your code
abaxyzzyxf
xyzzyx