# 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
```

One possible longest palindromic subsequence is "bb".

分析

```
dp[i,j]当前范围内最大的回文。
State transition:
dp[i][j] = dp[i+1][j-1] + 2 if s.charAt(i) == s.charAt(j)
otherwise, dp[i][j] = Math.max(dp[i+1][j], dp[i][j-1])
Initialization: dp[i][i] = 1
```

```
class Solution:
    def longestPalindromeSubseq(self, s: str) -> int:
        n = len(s)
        f = [[0]*(n+1) for _ in range(n+1)]

        for i in reversed(range(n+1)):
            f[i][i] =1
            for j in range(i+1,n+1):                
                if s[i-1] == s[j-1]:
                    f[i][j] = f[i+1][j-1]+2
                else:
                    f[i][j] = max(f[i+1][j],f[i][j-1])

        return f[1][n]
```

用start 和len来Loop

```
class Solution:
    def longestPalindromeSubseq(self, s: str) -> int:
        n = len(s)
        f = [[0]*(n+1) for _ in range(n+1)]
        for i in range(n+1):
            f[i][i] = 1

        for ln in range(1,n+1):            
            for start in range(1,n+1-ln):                
                if s[start-1] == s[start+ln-1]:
                    f[start][start+ln] = f[start+1][start+ln-1]+2
                else:
                    f[start][start+ln] = max(f[start+1][start+ln],f[start][start+ln-1])

        return f[1][n]
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nataliekung.gitbook.io/ladder_code/l4dong-tai-gui-hua/longest-palindromic-subsequence.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
