Problem Description
Given a string s, determine the length of its longest palindromic subsequence. A subsequence is a sequence derived from the string by deleting some or no characters while keeping the original order.
Key Insights
- The problem can be solved efficiently using dynamic programming.
- A 2D DP table is used where dp[i][j] represents the length of the longest palindromic subsequence in the substring s[i...j].
- If the characters at the two ends of the substring match (s[i] == s[j]), they contribute to the palindrome length.
- Otherwise, the solution is the maximum sequence length by either excluding the left or the right character.
- The approach builds the solution from smaller substrings (length 1) to the entire string.
Space and Time Complexity
Time Complexity: O(n^2) where n is the length of the string. Space Complexity: O(n^2) due to the usage of a 2D DP table.
Solution
We solve this problem using a bottom-up dynamic programming approach. We create a 2D table dp[][] such that dp[i][j] stores the longest palindromic subsequence length for the substring s[i...j]. For any single character, dp[i][i] is 1. For substrings of length greater than 1, if the characters at positions i and j (s[i] and s[j]) match, then dp[i][j] = dp[i+1][j-1] + 2. If they do not match, we take the maximum of dp[i+1][j] and dp[i][j-1]. Finally, the dp[0][n-1] holds the result for the entire string.