Maximize Palindrome Length From Subsequences LeetCode solution in Python
1class Solution:2def longestPalindrome(self, word1: str, word2: str) -> int:3res = 04new_word = word1 + word25n, mid = len(word1) + len(word2), len(word1)6dp = [[0] * n for _ in range(n)]7for i in range(n):8dp[i][i] = 19for l in range(n - 2, -1, -1):10for r in range(l + 1, n, 1):11if new_word[l] == new_word[r]:12dp[l][r] = (dp[l + 1][r - 1] if r - 1 >= l + 1 else 0) + 213if l < mid and r >= mid:14res = max(res, dp[l][r])15else:16dp[l][r] = max(dp[l + 1][r], dp[l][r - 1])17return res