|
| 1 | +--- |
| 2 | +id: longest-palindromic-subsequence |
| 3 | +title: Longest Palindromic Subsequence |
| 4 | +sidebar_label: 0516-Longest-Palindromic-Subsequence |
| 5 | +tags: |
| 6 | +- Dynamic Programming |
| 7 | +- String |
| 8 | +description: "Given a string s, find the longest palindromic subsequence's length in s." |
| 9 | +--- |
| 10 | + |
| 11 | +## Problem |
| 12 | + |
| 13 | +Given a string `s`, find the longest palindromic subsequence's length in `s`. |
| 14 | + |
| 15 | +A **subsequence** is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. |
| 16 | + |
| 17 | +### Examples |
| 18 | + |
| 19 | +**Example 1:** |
| 20 | + |
| 21 | +**Input:** `s = "bbbab"` |
| 22 | +**Output:** `4` |
| 23 | +**Explanation:** One possible longest palindromic subsequence is "bbbb". |
| 24 | + |
| 25 | +**Example 2:** |
| 26 | + |
| 27 | +**Input:** `s = "cbbd"` |
| 28 | +**Output:** `2` |
| 29 | +**Explanation:** One possible longest palindromic subsequence is "bb". |
| 30 | + |
| 31 | +### Constraints |
| 32 | + |
| 33 | +- `1 <= s.length <= 1000` |
| 34 | +- `s` consists only of lowercase English letters. |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +## Approach |
| 39 | + |
| 40 | +To solve this problem, we can use dynamic programming. We will create a 2D array `dp` where `dp[i][j]` represents the length of the longest palindromic subsequence in the substring `s[i:j+1]`. |
| 41 | + |
| 42 | +### Steps: |
| 43 | + |
| 44 | +1. Initialize a 2D array `dp` of size `n x n` where `n` is the length of the string `s`. Set all elements to 0. |
| 45 | +2. For each single character, set `dp[i][i] = 1` because a single character is a palindrome of length 1. |
| 46 | +3. Fill the `dp` array in a bottom-up manner: |
| 47 | + - For each substring length `l` from 2 to `n`: |
| 48 | + - For each starting index `i` from 0 to `n-l`: |
| 49 | + - Set `j = i + l - 1`. |
| 50 | + - If `s[i] == s[j]`, then `dp[i][j] = dp[i+1][j-1] + 2`. |
| 51 | + - Otherwise, `dp[i][j] = max(dp[i+1][j], dp[i][j-1])`. |
| 52 | +4. The result will be `dp[0][n-1]`, the length of the longest palindromic subsequence in the entire string. |
| 53 | + |
| 54 | +### Solution |
| 55 | + |
| 56 | +#### Java Solution |
| 57 | + |
| 58 | +```java |
| 59 | +class Solution { |
| 60 | + public int longestPalindromeSubseq(String s) { |
| 61 | + int n = s.length(); |
| 62 | + int[][] dp = new int[n][n]; |
| 63 | + |
| 64 | + for (int i = n - 1; i >= 0; i--) { |
| 65 | + dp[i][i] = 1; |
| 66 | + for (int j = i + 1; j < n; j++) { |
| 67 | + if (s.charAt(i) == s.charAt(j)) { |
| 68 | + dp[i][j] = dp[i + 1][j - 1] + 2; |
| 69 | + } else { |
| 70 | + dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return dp[0][n - 1]; |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | +#### C++ Solution |
| 80 | + |
| 81 | +```cpp |
| 82 | +class Solution { |
| 83 | +public: |
| 84 | + int longestPalindromeSubseq(string s) { |
| 85 | + int n = s.length(); |
| 86 | + vector<vector<int>> dp(n, vector<int>(n, 0)); |
| 87 | + |
| 88 | + for (int i = n - 1; i >= 0; i--) { |
| 89 | + dp[i][i] = 1; |
| 90 | + for (int j = i + 1; j < n; j++) { |
| 91 | + if (s[i] == s[j]) { |
| 92 | + dp[i][j] = dp[i + 1][j - 1] + 2; |
| 93 | + } else { |
| 94 | + dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return dp[0][n - 1]; |
| 100 | + } |
| 101 | +}; |
| 102 | +``` |
| 103 | +#### Python Solution |
| 104 | +
|
| 105 | +```python |
| 106 | +class Solution: |
| 107 | + def longestPalindromeSubseq(self, s: str) -> int: |
| 108 | + n = len(s) |
| 109 | + dp = [[0] * n for _ in range(n)] |
| 110 | + |
| 111 | + for i in range(n - 1, -1, -1): |
| 112 | + dp[i][i] = 1 |
| 113 | + for j in range(i + 1, n): |
| 114 | + if s[i] == s[j]: |
| 115 | + dp[i][j] = dp[i + 1][j - 1] + 2 |
| 116 | + else: |
| 117 | + dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]) |
| 118 | + |
| 119 | + return dp[0][n - 1] |
| 120 | +``` |
| 121 | +### Complexity Analysis |
| 122 | +**Time Complexity:** O(n^2) |
| 123 | +>Reason: We are filling an n x n table, and each cell takes constant time to compute. |
| 124 | + |
| 125 | +**Space Complexity:** O(n^2) |
| 126 | +>Reason: We use a 2D array dp of size n x n to store the intermediate results. |
| 127 | +
|
| 128 | +### References |
| 129 | +**LeetCode Problem:** Longest Palindromic Subsequence |
0 commit comments