Longest Palindromic Subsequence
Longest Palindromic Subsequence is LeetCode 516 (Medium). Given a string s, return the length of the longest subsequence of s that reads the same forwards and backwards.
A subsequence keeps the letters in their original order but may skip any of them, so the palindrome does not have to be one unbroken block.
- 1 <= s.length <= 1000
- s consists only of lowercase English letters.
Intuition
Work on a range of the string, from index i to index j, and look only at its two end letters.
- Ends equal: they can be the outer pair of a palindrome. Wrap them around the best palindrome strictly inside, which adds 2.
- Ends differ: no palindrome can use both, because its first and last letters would differ. Drop the left end or the right end and keep the better result.
Every range depends only on shorter ranges inside it, so filling the answers from short ranges to long ones solves the whole string in O(n²).
Approach
Define one cell per range
Let dp[i][j] be the answer for s[i..j], both ends included. Only cells with i <= j are used, so the table is a triangle. A single letter is a palindrome on its own, so dp[i][i] = 1.
Fill a range from its two ends
- If
s[i] == s[j]:dp[i][j] = dp[i+1][j-1] + 2. - Otherwise:
dp[i][j] = max(dp[i+1][j], dp[i][j-1]).
When j = i + 1 the inside range is empty, and the zero already stored at dp[i+1][i] is the right value for it.
Fill rows from the bottom up
A cell reads row i + 1, so run i from n - 1 down to 0, and j from i + 1 up to n - 1. That way every shorter range is finished before a longer one needs it. The answer is dp[0][n-1], the whole string.
Rebuild the palindrome if you need the letters
Start with the whole range. When the ends match, put that letter on both sides of the result and move both ends inwards. Otherwise move the end whose side holds the larger value. A single leftover letter becomes the middle.
Keep two rows to save memory
Row i reads only itself and row i + 1, so two arrays of length n give O(n) space when only the length is needed.
Code in Python, C++ and Java
Common pitfalls
Filling rows from the top down
for i in range(n):
dp[i][i] = 1
for j in range(i + 1, n):for i in range(n - 1, -1, -1):
dp[i][i] = 1
for j in range(i + 1, n):Row i reads row i + 1, which top-down order has not filled yet, so it reads zeros. On "bbbab" the wrong order returns 2 instead of 4, with no error to warn you.
Answering the substring question instead
only counting ranges that are palindromes themselves
letting a mismatch drop one end and keep the inside
A subsequence may skip letters. On "bbbab" the longest palindromic substring is "bbb" (3), but skipping the a gives "bbbb" (4).
Edge cases
"abcd"No pair of ends ever matches, so every range keeps the value of its best single letter and the answer is 1.
Complexity
Longest Palindromic Subsequence FAQ
Can I solve it with LCS on the string and its reverse?
Yes. A palindrome reads the same both ways, so any palindromic subsequence of s also appears in reversed s, and the longest subsequence the two share has the same length as the longest palindrome. For "bbbab" and "babbb" that gives 4. See Longest Common Subsequence for that table. It costs the same O(n²) but fills a full square instead of a triangle.
How many insertions make a string a palindrome?
n minus this answer. The letters in the longest palindromic subsequence can stay, and every other letter needs a mirror copy inserted. "mbadm" has length 5 and answer 3 ("mam"), so 2 insertions are enough. That is Minimum Insertion Steps to Make a String Palindrome.