MediumTwo PointersStringDynamic Programming

Longest Palindromic Substring

LeetCode
1 approach, code in all languages

Given a string, find the longest contiguous slice of it that is a palindrome, meaning it reads identically in both directions.

If several substrings share the maximum length, returning any one of them is acceptable. A single character is trivially a palindrome, so the answer is never empty when the input is non-empty.

Example 1

Input: s = "babad"

Output: "bab"

The slice "bab" is a palindrome of length three; "aba" would also be a valid answer.

Example 2

Input: s = "cbbd"

Output: "bb"

The longest mirror-image slice is "bb", since no palindrome of length three or more exists here.

Constraints

  • 1 ≤ s.length ≤ 1000
  • s consists of digits and English letters.
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now