516. Longest Palindromic Subsequence - Explanation
Description
You are given a string s, find the longest palindromic subsequence's length in s.
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.
Example 1:
Input: s = "bbbab"
Output: 4Explanation: One possible longest palindromic subsequence is "bbbb".
Example 2:
Input: s = "cbbd"
Output: 2Explanation: One possible longest palindromic subsequence is "bb".
Constraints:
1 <= s.length <= 1000sconsists only of lowercase English letters.
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- Dynamic Programming (2D) - The solution requires a 2D DP table to track palindrome lengths for substrings defined by two indices
- Recursion with Memoization - Top-down approaches use recursive calls with caching to avoid recomputation
- Longest Common Subsequence (LCS) - One approach reduces this problem to finding the LCS between the string and its reverse
- String Manipulation - Understanding how to work with substrings and character comparisons
1. Dynamic Programming (Top Down)
Intuition
A palindrome reads the same forwards and backwards, so we can think of building one by expanding outward from a center. For each possible center (single character for odd length, between two characters for even length), we try to extend the palindrome by checking if the characters on both sides match.
If the characters match, we include both in our subsequence and continue expanding. If they don't match, we have a choice: skip the left character or skip the right character, and take whichever gives a longer result. Memoization prevents us from recomputing the same subproblems.
Algorithm
- Create a 2D memoization table
dpwheredp[i][j]stores the longest palindromic subsequence that can be formed starting at indexiand ending at indexj. - Define a recursive function
dfs(i, j)that:- Returns
0if indices are out of bounds. - Returns the cached result if already computed.
- If characters at
iandjmatch, adds1(if same index) or2(different indices) plus the result of expanding outward. - Otherwise, takes the maximum of skipping either the left or right character.
- Returns
- Call
dfsfor all possible centers (both odd and even length palindromes). - Return the maximum value found in the DP table.
class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
n = len(s)
dp = [[-1] * n for _ in range(n)]
def dfs(i, j):
if i < 0 or j == n:
return 0
if dp[i][j] != -1:
return dp[i][j]
if s[i] == s[j]:
length = 1 if i == j else 2
dp[i][j] = length + dfs(i - 1, j + 1)
else:
dp[i][j] = max(dfs(i - 1, j), dfs(i, j + 1))
return dp[i][j]
for i in range(n):
dfs(i, i) # odd length
dfs(i, i + 1) # even length
return max(max(row) for row in dp if row != -1)Time & Space Complexity
- Time complexity:
- Space complexity:
2. Dynamic Programming (Top-Down Optimized)
Intuition
Instead of expanding from centers, we can approach this problem by considering the entire string and shrinking inward. We define our subproblem as finding the longest palindromic subsequence within the substring from index i to index j.
When the first and last characters match, they can both be part of our palindrome, so we add 2 and solve for the inner substring. When they don't match, one of them must be excluded, so we try both options and take the better one.
Algorithm
- Create a memoization cache (hash map or 2D array) to store computed results.
- Define a recursive function
dfs(i, j)that:- Returns
0ifi > j(empty substring). - Returns
1ifi == j(single character is a palindrome of length1). - Returns the cached result if already computed.
- If
s[i] == s[j], returns2 + dfs(i+1, j-1). - Otherwise, returns
max(dfs(i+1, j), dfs(i, j-1)).
- Returns
- Call
dfs(0, n-1)to get the answer for the entire string.
class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
cache = {}
def dfs(i, j):
if i > j:
return 0
if i == j:
return 1
if (i, j) in cache:
return cache[(i, j)]
if s[i] == s[j]:
cache[(i, j)] = dfs(i + 1, j - 1) + 2
else:
cache[(i, j)] = max(dfs(i + 1, j), dfs(i, j - 1))
return cache[(i, j)]
return dfs(0, len(s) - 1)Time & Space Complexity
- Time complexity:
- Space complexity:
3. Dynamic Programming (Using LCS Idea)
Intuition
A clever observation: the longest palindromic subsequence of a string is the same as the longest common subsequence (LCS) between the string and its reverse. Why? Any palindromic subsequence appears in the same order when read forwards or backwards, making it a common subsequence of both strings.
This transforms our problem into the classic LCS problem, which has a well-known dynamic programming solution.
Algorithm
- Create the reverse of the input string.
- Use the standard LCS algorithm:
- Create a 2D DP table where
dp[i][j]represents the LCS length of the firsticharacters of the original string and the firstjcharacters of the reversed string. - If characters match,
dp[i+1][j+1] = dp[i][j] + 1. - Otherwise,
dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]).
- Create a 2D DP table where
- Return
dp[n][n]as the final answer.
class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
return self.longestCommonSubsequence(s, s[::-1])
def longestCommonSubsequence(self, s1: str, s2: str) -> int:
N, M = len(s1), len(s2)
dp = [[0] * (M + 1) for _ in range(N + 1)]
for i in range(N):
for j in range(M):
if s1[i] == s2[j]:
dp[i + 1][j + 1] = 1 + dp[i][j]
else:
dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1])
return dp[N][M]Time & Space Complexity
- Time complexity:
- Space complexity:
4. Dynamic Programming (Space Optimized)
Intuition
The bottom-up DP approach can be optimized for space. When filling the DP table, each cell only depends on cells from the current and previous rows (or in this formulation, the current row and the previous diagonal value). By processing in the right order and keeping track of just the necessary values, we can reduce space from O(n^2) to O(n).
Algorithm
- Create a 1D DP array of size
n. - Iterate
ifromn-1down to0:- Set
dp[i] = 1(single character palindrome). - Track
prevto store the previous diagonal value. - For each
jfromi+1ton-1:- Save current
dp[j]before updating. - If
s[i] == s[j], setdp[j] = prev + 2. - Otherwise, set
dp[j] = max(dp[j], dp[j-1]). - Update
prevwith the saved value.
- Save current
- Set
- Return
dp[n-1].
class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
n = len(s)
dp = [0] * n
for i in range(n - 1, -1, -1):
dp[i] = 1
prev = 0
for j in range(i + 1, n):
temp = dp[j]
if s[i] == s[j]:
dp[j] = prev + 2
else:
dp[j] = max(dp[j], dp[j - 1])
prev = temp
return dp[n - 1]Time & Space Complexity
- Time complexity:
- Space complexity:
Common Pitfalls
Confusing Subsequence with Substring
A subsequence does not require contiguous characters, while a substring does. For the string "bbbab", the longest palindromic subsequence is "bbbb" (length 4), but the longest palindromic substring is "bbb" (length 3). Make sure your solution allows skipping characters when they do not contribute to the palindrome.
Incorrect Base Case Handling
When implementing the recursive or DP solution, a common error is mishandling the base cases. A single character (i == j) is always a palindrome of length 1, and when i > j (empty range), the length is 0. Forgetting to return 1 for single characters or incorrectly initializing the DP table leads to off-by-one errors.
Wrong Iteration Order in Bottom-Up DP
In the bottom-up approach, the DP table must be filled in the correct order so that smaller subproblems are solved before larger ones. You need to iterate i from n-1 down to 0 and j from i to n-1. Iterating in the wrong direction causes the algorithm to read uncomputed values, producing incorrect results.
Sign in to join the discussion