<p>You are given two strings, <code>word1</code> and <code>word2</code>. You want to construct a string in the following manner:</p>

<ul>
	<li>Choose some <strong>non-empty</strong> subsequence <code>subsequence1</code> from <code>word1</code>.</li>
	<li>Choose some <strong>non-empty</strong> subsequence <code>subsequence2</code> from <code>word2</code>.</li>
	<li>Concatenate the subsequences: <code>subsequence1 + subsequence2</code>, to make the string.</li>
</ul>

<p>Return <em>the <strong>length</strong> of the longest <strong>palindrome</strong> that can be constructed in the described manner. </em>If no palindromes can be constructed, return <code>0</code>.</p>

<p>A <strong>subsequence</strong> of a string <code>s</code> is a string that can be made by deleting some (possibly none) characters from <code>s</code> without changing the order of the remaining characters.</p>

<p>A <strong>palindrome</strong> is a string that reads the same forward&nbsp;as well as backward.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> word1 = &quot;cacb&quot;, word2 = &quot;cbba&quot;
<strong>Output:</strong> 5
<strong>Explanation:</strong> Choose &quot;ab&quot; from word1 and &quot;cba&quot; from word2 to make &quot;abcba&quot;, which is a palindrome.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> word1 = &quot;ab&quot;, word2 = &quot;ab&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> Choose &quot;ab&quot; from word1 and &quot;a&quot; from word2 to make &quot;aba&quot;, which is a palindrome.</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> word1 = &quot;aa&quot;, word2 = &quot;bb&quot;
<strong>Output:</strong> 0
<strong>Explanation:</strong> You cannot construct a palindrome from the described method, so return 0.</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= word1.length, word2.length &lt;= 1000</code></li>
	<li><code>word1</code> and <code>word2</code> consist of lowercase English letters.</li>
</ul>

from collections import defaultdict
from bisect import bisect_left,bisect_right
class Solution:
    def longestPalindrome(self, word1: str, word2: str) -> int:
        dct1 = defaultdict(lambda:-1)
        dct2 = defaultdict(lambda:-1)
        def doItt(s,i,j,isFirst):
            if j<i:return 0
            if i==j:return 1
            if isFirst:
                if dct1[(i,j)]+1:return dct1[(i,j)]
                dct1[(i,j)] = max(doItt(s,i,j-1,isFirst),doItt(s,i+1,j,isFirst))
                if s[i]==s[j]:
                    dct1[(i,j)] = max(dct1[(i,j)],doItt(s,i+1,j-1,isFirst)+2)
                return dct1[(i,j)]
            else:
                if dct2[(i,j)]+1:return dct2[(i,j)]
                dct2[(i,j)] = max(doItt(s,i,j-1,isFirst),doItt(s,i+1,j,isFirst))
                if s[i]==s[j]:
                    dct2[(i,j)] = max(dct2[(i,j)],doItt(s,i+1,j-1,isFirst)+2)
                return dct2[(i,j)]
        doItt(word1,0,len(word1)-1,True)
        doItt(word2,0,len(word2)-1,False)
        dct = defaultdict(lambda:-1)
        arr = word1+word2
        ans = 0
        def doIt(i=0,j=len(arr)-1):
            nonlocal ans
            if j<=i:return max(j-i+1,0)
            if i>=len(word1):return dct2[(i-len(word1),j-len(word1))]
            if j<len(word1):return dct1[(i,j)]
            if dct[(i,j)]+1:return dct[(i,j)]
            if arr[i]==arr[j]:
                dct[i,j] = doIt(i+1,j-1)+2
                if i<len(word1) and j>=len(word1):
                    ans = max(ans,dct[i,j])
            else:
                dct[(i,j)] = max(doIt(i,j-1),doIt(i+1,j))
            return dct[(i,j)]
        doIt()
        return ans
2301