Skip to content
Onsite

Longest Palindromic Subsequence

MediumP2Coding

Return the length of the longest palindromic subsequence of s. A subsequence keeps the original order but may skip characters, and a palindrome reads the same forwards and backwards.

Example 1

Input
s = "bbbab"
Output
4
Why
"bbbb" is a subsequence of length 4.

Example 2

Input
s = "cbbd"
Output
2
Why
"bb" has length 2.

Example 3

Input
s = "a"
Output
1

Constraints

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

Hints

Run checks the 3 visible tests, Submit all 11.