java.lang.Object
g1701_1800.s1771_maximize_palindrome_length_from_subsequences.Solution

public class Solution extends Object
1771 - Maximize Palindrome Length From Subsequences.<p>Hard</p> <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 as well as backward.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> word1 = &ldquo;cacb&rdquo;, word2 = &ldquo;cbba&rdquo;</p> <p><strong>Output:</strong> 5</p> <p><strong>Explanation:</strong> Choose &ldquo;ab&rdquo; from word1 and &ldquo;cba&rdquo; from word2 to make &ldquo;abcba&rdquo;, which is a palindrome.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> word1 = &ldquo;ab&rdquo;, word2 = &ldquo;ab&rdquo;</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> Choose &ldquo;ab&rdquo; from word1 and &ldquo;a&rdquo; from word2 to make &ldquo;aba&rdquo;, which is a palindrome.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> word1 = &ldquo;aa&rdquo;, word2 = &ldquo;bb&rdquo;</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> You cannot construct a palindrome from the described method, so return 0.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= word1.length, word2.length <= 1000</code></li> <li><code>word1</code> and <code>word2</code> consist of lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • longestPalindrome

      public int longestPalindrome(String word1, String word2)