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)

One problem, two ways to prep

Choose between solo practice and interview simulation

Practice Mode keeps things simple with code + tests. AI Interview Mode adds voice, pressure, and a post-round score summary.