java.lang.Object
g2101_2200.s2131_longest_palindrome_by_concatenating_two_letter_words.Solution

public class Solution extends Object
2131 - Longest Palindrome by Concatenating Two Letter Words.<p>Medium</p> <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><strong>Example 1:</strong></p> <p><strong>Input:</strong> words = [&ldquo;lc&rdquo;,&ldquo;cl&rdquo;,&ldquo;gg&rdquo;]</p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong> One longest palindrome is &ldquo;lc&rdquo; + &ldquo;gg&rdquo; + &ldquo;cl&rdquo; = &ldquo;lcggcl&rdquo;, of length 6. Note that &ldquo;clgglc&rdquo; is another longest palindrome that can be created.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> words = [&ldquo;ab&rdquo;,&ldquo;ty&rdquo;,&ldquo;yt&rdquo;,&ldquo;lc&rdquo;,&ldquo;cl&rdquo;,&ldquo;ab&rdquo;]</p> <p><strong>Output:</strong> 8</p> <p><strong>Explanation:</strong> One longest palindrome is &ldquo;ty&rdquo; + &ldquo;lc&rdquo; + &ldquo;cl&rdquo; + &ldquo;yt&rdquo; = &ldquo;tylcclyt&rdquo;, of length 8. Note that &ldquo;lcyttycl&rdquo; is another longest palindrome that can be created.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> words = [&ldquo;cc&rdquo;,&ldquo;ll&rdquo;,&ldquo;xx&rdquo;]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> One longest palindrome is &ldquo;cc&rdquo;, of length 2. Note that &ldquo;ll&rdquo; is another longest palindrome that can be created, and so is &ldquo;xx&rdquo;.</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>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • longestPalindrome

      public int longestPalindrome(String[] words)