A subsequence of a string s is considered a good palindromic subsequence if:
s.For example, if s = "abcabcabb", then "abba" is considered a good palindromic subsequence, while "bcb" (not even length) and "bbbb" (has equal consecutive characters) are not.
Given a string s, return the length of the longest good palindromic subsequence in s.
Example 1:
Input: s = "bbabab" Output: 4 Explanation: The longest good palindromic subsequence of s is "baab".
Example 2:
Input: s = "dcbccacdb" Output: 4 Explanation: The longest good palindromic subsequence of s is "dccd".
Constraints:
1 <= s.length <= 250s consists of lowercase English letters.Problem Overview: Given a string, return the length of the longest palindromic subsequence such that no two adjacent characters in the subsequence are the same. You can delete characters but must keep the relative order. The main challenge is enforcing both the palindrome structure and the adjacent-character constraint.
Approach 1: Brute Force Subsequence Enumeration (Exponential Time)
Generate every possible subsequence and check two conditions: whether the subsequence forms a palindrome and whether adjacent characters are different. Subsequence generation requires iterating through all 2^n combinations. Each candidate then needs a palindrome check and a linear scan to verify the adjacent constraint. This approach runs in O(2^n * n) time and O(n) space for recursion. It quickly becomes infeasible once the string length grows, but it helps clarify the problem constraints before introducing dynamic programming.
Approach 2: Memoized Search with Character State (Dynamic Programming) (O(n^2 * 26))
Use a top-down DP where the state is dfs(i, j, prev). The indices i and j define the current substring, and prev stores the character used in the outer palindrome layer. This extra state ensures the next chosen pair does not repeat the same character consecutively. If s[i] == s[j] and s[i] != prev, you can include both characters and recurse into dfs(i+1, j-1, s[i]). Otherwise, skip either side using dfs(i+1, j) or dfs(i, j-1). Memoizing results avoids recomputing overlapping subproblems.
The state space contains roughly n * n * 26 possibilities because the previous character can be one of the 26 lowercase letters (or none initially). Each state performs constant work, leading to O(n^2 * 26) time complexity and O(n^2 * 26) space for the memo table. This pattern is common in dynamic programming problems where additional constraints require expanding the DP state.
The key insight: standard longest palindromic subsequence DP only tracks substring boundaries. This problem adds a constraint about adjacent characters, so you must also track the previously chosen character. Combining substring DP with memoization keeps the search efficient while enforcing the rule.
This solution heavily relies on recursion with caching, a classic technique in dynamic programming and string problems involving subsequences.
Recommended for interviews: The memoized dynamic programming approach. Brute force demonstrates understanding of subsequences and palindrome validation, but the optimized DP shows you can identify overlapping subproblems and extend the state to enforce constraints. Interviewers expect the O(n^2 * alphabet) DP with memoization.
We design a function dfs(i, j, x) to represent the length of the longest "good" palindrome subsequence ending with character x in the index range [i, j] of string s. The answer is dfs(0, n - 1, 26).
The calculation process of the function dfs(i, j, x) is as follows:
i >= j, then dfs(i, j, x) = 0;s[i] = s[j] and s[i] neq x, then dfs(i, j, x) = dfs(i + 1, j - 1, s[i]) + 2;s[i] neq s[j], then dfs(i, j, x) = max(dfs(i + 1, j, x), dfs(i, j - 1, x)).During the process, we can use memorization search to avoid repeated calculations.
The time complexity is O(n^2 times C). Where n is the length of the string s, and C is the size of the character set. In this problem, C = 26.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Subsequence Enumeration | O(2^n * n) | O(n) | Conceptual understanding of subsequences and constraints |
| Memoized DFS with Previous Character State | O(n^2 * 26) | O(n^2 * 26) | General solution expected in interviews |
| Bottom-Up 3D Dynamic Programming | O(n^2 * 26) | O(n^2 * 26) | When avoiding recursion or implementing iterative DP |
Leetcode 1682. Longest Palindromic Subsequence II • Algorithms for Big Bucks • 383 views views
Watch 2 more video solutions →Practice Longest Palindromic Subsequence II with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor