Skip to content

Longest Palindromic Subsequence

MediumDynamic Programming
Decode it first. Clues, constraints, pattern and plan before any code.

Return the length of the longest subsequence of s that is a palindrome. A subsequence keeps the original order but may skip characters.

s = "bbbab"   →  4   ("bbbb")
s = "cbbd"    →  2   ("bb")
s = "agbdba"  →  5   ("abdba")

Constraints

  • 1 <= s.length <= 1000
  • s contains only lowercase letters.