All problems
MediumStrings

Longest Palindromic Substring

amazonmicrosoftgooglemetaadobeoracle

Given a string s, return the longest palindromic substring in s.

A palindrome is a string that reads the same forward and backward.

Example 1:

Input: s = "babad"
Output: "bab"
Explanation: "aba" is also a valid answer.

Example 2:

Input: s = "cbbd"
Output: "bb"

Example 3:

Input: s = "a"
Output: "a"

Examples

Example 1

Input: s = "babad"

Output: "bab"

Explanation: "bab" is a palindromic substring of length 3. "aba" is also a valid answer.

Example 2

Input: s = "cbbd"

Output: "bb"

Explanation: "bb" is the longest palindromic substring with length 2.

Constraints

  • -1 <= s.length <= 1000
  • -s consist of only digits and English letters.

Optimal Complexity

Time

O(n^2)

Space

O(1)

Practice this problem with an AI interviewer

TechInView conducts a full voice mock interview — the AI asks clarifying questions, evaluates your approach, watches you code, and scores you on 5 dimensions. Just like a real FAANG interview.

Start a free interview