Longest Palindromic Subsequence II - Problem

A subsequence of a string s is considered a good palindromic subsequence if:

  • It is a subsequence of s.
  • It is a palindrome (has the same value if reversed).
  • It has an even length.
  • No two consecutive characters are equal, except the two middle ones.

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.

Input & Output

Example 1 — Basic Case
$ Input: s = "abcabc"
Output: 4
💡 Note: The good palindromic subsequence "abba" can be formed by taking characters at indices 0,1,3,4 → a,b,b,a. Length is 4, it's a palindrome, has even length, and no consecutive duplicates except the middle bb.
Example 2 — No Valid Subsequence
$ Input: s = "aaa"
Output: 0
💡 Note: Cannot form any good palindromic subsequence. "aa" would have consecutive equal characters but they're not in the middle positions.
Example 3 — Longer String
$ Input: s = "bbcacb"
Output: 4
💡 Note: The good palindromic subsequence "bcab" can be formed by taking characters at indices 0,2,3,5 → b,c,a,b. Wait, that's "bcab" which is not a palindrome. Actually "bccb" from indices 0,1,2,5 gives b,b,c,b - not valid due to consecutive b's. The valid one is "bccb" but rearranged properly.

Constraints

  • 1 ≤ s.length ≤ 250
  • s consists of lowercase English letters only

Visualization

Tap to expand
Longest Palindromic Subsequence II INPUT String s = "abcabc" a 0 b 1 c 2 a 3 b 4 c 5 Good Palindrome Rules: 1. Even length 2. No consecutive equal chars 3. Except middle two chars 4. Is a palindrome Example Subsequences: "abba" - Valid (len 4) "abca" - Valid (len 4) "aa" - Invalid (consecutive) ALGORITHM STEPS 1 Define DP State dp[i][j][c] = longest good palindrome in s[i..j] ending c 2 Base Case dp[i][i][c] = 0 (odd len) Single char not valid 3 Transition If s[i]==s[j] and s[i]!=c: dp[i][j][s[i]] = 2 + max(dp[i+1][j-1][k]) 4 Compute Answer Return max(dp[0][n-1][c]) for all characters c DP Table Structure dp[i][j][last_char] i,j: substring range last_char: outer char used FINAL RESULT Longest Good Palindrome: a b b a palindrome Output: 4 Length of "abba" Verification: [OK] Even length: 4 [OK] Palindrome: abba [OK] No consecutive equal (except middle bb) [OK] Subsequence of s Key Insight: The 3D DP tracks the last character used at boundaries to ensure no consecutive equal characters. When s[i] == s[j] and differs from last used char, we can extend the palindrome by 2. Time Complexity: O(n^2 * 26) | Space Complexity: O(n^2 * 26) TutorialsPoint - Longest Palindromic Subsequence II | Optimal DP Solution
Asked in
Google 15 Facebook 12 Amazon 8
12.0K Views
Medium Frequency
~35 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen