# Problem

LeetCode #516. Longest Palindromic Subsequence

Given a string s, find the longest palindromic subsequence’s length in s. You may assume that the maximum length of s is 1000.

Example 1:

Input:

"bbbab"

Output:

4

One possible longest palindromic subsequence is “bbbb”.

Example 2:

Input:

"cbbd"

Output:

2

# Explanation

This problem can be compared to the Longest Palindromic Substring. In that problem, we should find the substring of the given string, while in this problem, we only need to find the subsequence. So, in that situation, we can use boolean array to check whether the substring from i to j is palindromic. However, in this problem, we have to save the length, as the palindromic can be discontinuous.

Similarly, dp[i][j] means the longest palindrome between the scope i to j.

# Solution

Time complexity is O(n^2)

Space complexity is O(n^2)

1 | class Solution { |