how to find longest palindromic subsequence?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The longest palindromic subsequence problem asks for the longest sequence of characters that reads the same forward and backward, without requiring the characters to stay contiguous. The standard solution uses dynamic programming because the problem has overlapping subproblems and a clean recurrence.
Subsequence Versus Substring
A subsequence can skip characters, while a substring must stay contiguous. For example, in bbbab, the longest palindromic subsequence is bbbb, even though those four characters are not all adjacent.
That distinction is why algorithms for longest palindromic substring do not directly solve longest palindromic subsequence.
Dynamic Programming Recurrence
Let dp[i][j] be the length of the longest palindromic subsequence in the slice from index i to index j.
The recurrence is:
- if
s[i] == s[j], thendp[i][j] = dp[i + 1][j - 1] + 2 - otherwise,
dp[i][j] = max(dp[i + 1][j], dp[i][j - 1])
The base case is that every single character is a palindrome of length 1, so dp[i][i] = 1.
Bottom-Up Implementation
A bottom-up table is usually the clearest implementation.
This runs in O(n^2) time and uses O(n^2) space.
Reconstruct The Actual Subsequence
Sometimes the length is not enough. You may also want the subsequence itself. One way is to walk backward through the DP table.
The reconstruction step follows the same logic used to fill the table.
Why The Recurrence Works
If the two end characters match, any best subsequence inside the smaller range can be extended by those two matching characters. If the ends do not match, the optimal subsequence must exclude one end or the other, so the answer is the best of those two smaller ranges.
This is exactly the kind of local choice dynamic programming handles well.
Common Pitfalls
A common mistake is confusing the problem with longest palindromic substring. Substring algorithms depend on contiguity, but subsequences can skip positions.
Another mistake is filling the DP table in the wrong order. Since dp[i][j] depends on smaller ranges, the table must be built from short slices to long slices.
It is also easy to forget the length == 2 case when two equal adjacent characters appear. Without that case, the lookup to the inner subproblem can be awkward.
Summary
- Longest palindromic subsequence is a dynamic programming problem, not a greedy one.
- Use
dp[i][j]to store the best answer for each substring range. - The standard recurrence gives an
O(n^2)time solution. - You can reconstruct the actual subsequence by walking the finished table.
- Do not confuse subsequences with substrings because the algorithms are different.
Related reading
- How to find max. and min. in array using minimum comparisons?
- How to find maximum spanning tree?
- How to find minimum number of jumps to reach the end of the array in On time
- How to find minimum positive contiguous sub sequence in On time?
- How to find mother vertex in a directed graph in Onm?
- How to find multidimensional path of exact 0 cost with 1, 0, -1 weights
- How to find nth element from the end of a singly linked list?
- How to find out Geometric Median

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.