mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
45 lines
2.0 KiB
HTML
45 lines
2.0 KiB
HTML
<p>You are given an array of strings <code>words</code>. Each element of <code>words</code> consists of <strong>two</strong> lowercase English letters.</p>
|
|
|
|
<p>Create the <strong>longest possible palindrome</strong> by selecting some elements from <code>words</code> and concatenating them in <strong>any order</strong>. Each element can be selected <strong>at most once</strong>.</p>
|
|
|
|
<p>Return <em>the <strong>length</strong> of the longest palindrome that you can create</em>. If it is impossible to create any palindrome, return <code>0</code>.</p>
|
|
|
|
<p>A <strong>palindrome</strong> is a string that reads the same forward and backward.</p>
|
|
|
|
<p> </p>
|
|
<p><strong>Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> words = ["lc","cl","gg"]
|
|
<strong>Output:</strong> 6
|
|
<strong>Explanation:</strong> One longest palindrome is "lc" + "gg" + "cl" = "lcggcl", of length 6.
|
|
Note that "clgglc" is another longest palindrome that can be created.
|
|
</pre>
|
|
|
|
<p><strong>Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> words = ["ab","ty","yt","lc","cl","ab"]
|
|
<strong>Output:</strong> 8
|
|
<strong>Explanation:</strong> One longest palindrome is "ty" + "lc" + "cl" + "yt" = "tylcclyt", of length 8.
|
|
Note that "lcyttycl" is another longest palindrome that can be created.
|
|
</pre>
|
|
|
|
<p><strong>Example 3:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> words = ["cc","ll","xx"]
|
|
<strong>Output:</strong> 2
|
|
<strong>Explanation:</strong> One longest palindrome is "cc", of length 2.
|
|
Note that "ll" is another longest palindrome that can be created, and so is "xx".
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= words.length <= 10<sup>5</sup></code></li>
|
|
<li><code>words[i].length == 2</code></li>
|
|
<li><code>words[i]</code> consists of lowercase English letters.</li>
|
|
</ul>
|