AlgoFrames

Longest palindromic subsequence

Standard · Write code · about 20 min

Return the length of the longest palindromic subsequence of s. A subsequence keeps the original order but may skip characters.

Example 1

Input:

["bbbab"]

Output:

4

bbbb is one longest palindromic subsequence.

Example 2

Input:

["cbbd"]

Output:

2

bb.

Constraints

  • 1 ≤ s.length ≤ 1000
  • s uses lowercase English letters.

Learn the idea first: String DP: LCS, edit distance, palindromic subsequence, a visual lesson.

More String DP: LCS, edit distance, palindromic subsequence problems