<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>

class Solution:
    def longestPalindrome(self, word1: str, word2: str) -> int:
        # DP
        # dp[i][j] is the length of longest palindrome between indices i and j
        # of the merged string
        # dp[>= i][i] = 0
        # dp[i][i + 1] = 1
        # dp[0][len(word1) + len(word2)] is answer
        merged = word1 + word2
        dp = [[0 for _ in range(len(merged) + 1)] for _ in range(len(merged))]
        for i in range(len(merged)):
            dp[i][i + 1] = 1
        
        letterToInds = dict()
        for i in range(len(merged)):
            c = merged[i]
            if c not in letterToInds:
                letterToInds[c] = []
            letterToInds[c].append(i)
        
        def findLargestIndexLessThanJ(j, inds):
            low = 0
            high = len(inds) - 1
            while low < high:
                mid = (low + high + 1) // 2
                if inds[mid] == j:
                    return inds[mid - 1]
                elif inds[mid] > j:
                    high = mid - 1
                else:
                    low = mid
            return inds[low]
        
        for diff in range(2, len(merged) + 1):
            for i in range(0, len(merged) + 1 - diff):
                j = i + diff # upper limit exclusive
                firstChar = merged[i]
                inds = letterToInds[firstChar]
                k = findLargestIndexLessThanJ(j, inds)
                if k == i:
                    # couldn't find this letter in rest
                    dp[i][j] = dp[i + 1][j]
                    continue
                dp[i][j] = max(2 + dp[i + 1][k], dp[i + 1][j])
        
        # now make sure both are nonempty
        maxSeen = 0
        for i in range(len(word1)):
            c = word1[i]
            if letterToInds[c][-1] < len(word1):
                continue
            maxSeen = max(maxSeen, 2 + dp[i + 1][letterToInds[c][-1]])
        return maxSeen
85600