Talentd
FleetCode

1682. Longest Palindromic Subsequence II

Medium50.1% acceptancePremium elsewhere, free here

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.

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 <= 250
  • s consists of lowercase English letters.

Hints open one at a time

Hint 1
As with any good dp problem that uses palindromes, try building the palindrome from the edges
Hint 2
The prime point is to check that no two adjacent characters are equal, so save the past character while building the palindrome.
9:49
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
"bbabab"
Input
"dcbccacdb"
Your code is saved on this device.Solution