Longest Palindromic Subsequence II

problem [https://leetcode.com/problems/longest-palindromic-subsequence-ii/ 1682. Longest Palindromic Subsequence II].

Problem description
A subsequence of a string  is considered a good palindromic subsequence if:
 * It is a subsequence of.
 * 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, then   is considered a good palindromic subsequence, while   (not even length) and   (has equal consecutive characters) are not.

Given a string, return the length of the longest good palindromic subsequence in.


 * 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
 * consists of lowercase English letters.
 * consists of lowercase English letters.

Problem analysis
We can use dynamic programming to find out the longest palindromic substring in a similar fashion as in the Longest Palindromic Subsequence problem. But there are two additional constraints here:
 * The length of good palindromes must be even.
 * No two adjacent characters are the same, except for the two characters in the middle.

So we have to store an additional piece of information: the edge characters for the palindromic substrings.

We can define a class  as follows:
 * means the maximum length of good palindromic subsequences contained in.
 * means the edge character of the longest good palindromic subsequences.
 * Put the null character '\0' if  is 0, or if   > 0 and there are more than one good palindromic subsequences of length  . In this case we can always manage to extend the palindrome without concerning about its edge characters.
 * means whether we can extend the good palindromic subsequence by adding character  on both sides.
 * We cannot extend if there is exactly one good palindromic subsequence of length, and this palindrome's edge character is equal to.
 * Operators ==, >, and < are overloaded to compare the two cells. (C++ only)

Then we can start solving the problem using dynamic programming.

Let  be the length of the input string. We first build the  table with   rows and   columns. Let  be the cell containing the information about the maximal good palindromic subsequence on.

The transition equations are as follows:
 * Base case:,.
 * If :
 * If, this means we can possibly extend the palindromic subsequence using the edge characters. We need to check the down-left cell.
 * If  is the same as , then it would be inevitable that we will make two adjacent characters in the palindrome be the same, which is not allowed. So we copy   and   into.
 * Otherwise we will not be constrained by the edge characters. We put  and.
 * Otherwise we cannot extend the longest good palindrome in any substring of . So we look into the left cell   and the down cell.
 * If the down cell and the right cell has the same, we make use of the  . Then check the   of the two cells: if they are the same then we use that  ; otherwise we set   to null character (there are two different subsequences of   with different edge characters, which will not preclude us from extending the palindromes).
 * Otherwise pick the cell with greater, and copy its.

We can start filling up the table from, and then increase   step-wise until it matches the input length.

The final returned result is.

Complexity analysis
Let  be the input string's length.


 * Space complexity
 * Since we created the  table, the space complexity is O(len2).


 * Time complexity
 * Since we created the  table, and each cell requires constant computation, the time complexity is O(len2).