Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Longest Palindromic Substring

Medium Acceptance 34.80% Points 30.00

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.

Hints
Hint 1
Expected Time Complexity: O(n^2)
Hint 2
Expected Space Complexity: O(1)
Constraints
  • 1 <= |S| <= 10^4
Companies
Amazon Microsoft Samsung Visa Walmart + 2 more
Topics
String
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions