noodleProblems/
Longest Palindromic Substring
#05

Longest Palindromic Substring

AlgorithmmediumTwo PointersStringDynamic Programming

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

A substring reads the same forwards and backwards. If several substrings tie for the longest, return any one of them — any valid longest palindrome is accepted, not one specific string.

Example cases

  • two answers
    in s = "babad"
    out "bab"
    "aba" is also accepted — both are length-3 palindromes.
  • even length
    in s = "cbbd"
    out "bb"
  • single char
    in s = "a"
    out "a"

Constraints

  • 1 <= s.length <= 1000
  • s consists of digits and English letters.
Saved
s =
"babad"