1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/由子序列构造的最长回文串的长度 [maximize-palindrome-length-from-subsequences].html
2022-03-29 12:43:11 +08:00

43 lines
1.8 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你两个字符串 <code>word1</code><code>word2</code> ,请你按下述方法构造一个字符串:</p>
<ul>
<li><code>word1</code> 中选出某个 <strong>非空</strong> 子序列 <code>subsequence1</code></li>
<li><code>word2</code> 中选出某个 <strong>非空</strong> 子序列 <code>subsequence2</code></li>
<li>连接两个子序列 <code>subsequence1 + subsequence2</code> ,得到字符串。</li>
</ul>
<p>返回可按上述方法构造的最长 <strong>回文串</strong><strong>长度</strong> 。如果无法构造回文串,返回 <code>0</code></p>
<p>字符串 <code>s</code> 的一个 <strong>子序列</strong> 是通过从 <code>s</code> 中删除一些(也可能不删除)字符而不更改其余字符的顺序生成的字符串。</p>
<p><strong>回文串</strong> 是正着读和反着读结果一致的字符串。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>word1 = "cacb", word2 = "cbba"
<strong>输出:</strong>5
<strong>解释:</strong>从 word1 中选出 "ab" ,从 word2 中选出 "cba" ,得到回文串 "abcba" 。</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>word1 = "ab", word2 = "ab"
<strong>输出:</strong>3
<strong>解释:</strong>从 word1 中选出 "ab" ,从 word2 中选出 "a" ,得到回文串 "aba" 。</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>word1 = "aa", word2 = "bb"
<strong>输出:</strong>0
<strong>解释:</strong>无法按题面所述方法构造回文串,所以返回 0 。</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= word1.length, word2.length &lt;= 1000</code></li>
<li><code>word1</code><code>word2</code> 由小写英文字母组成</li>
</ul>