{"id":9418,"date":"2022-01-09T04:02:42","date_gmt":"2022-01-09T12:02:42","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9418"},"modified":"2022-01-09T04:03:34","modified_gmt":"2022-01-09T12:03:34","slug":"leetcode-2131-longest-palindrome-by-concatenating-two-letter-words","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2131-longest-palindrome-by-concatenating-two-letter-words\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2131. Longest Palindrome by Concatenating Two Letter Words"},"content":{"rendered":"\n<p>You are given an array of strings\u00a0<code>words<\/code>. Each element of\u00a0<code>words<\/code>\u00a0consists of\u00a0<strong>two<\/strong>\u00a0lowercase English letters.<\/p>\n\n\n\n<p>Create the&nbsp;<strong>longest possible palindrome<\/strong>&nbsp;by selecting some elements from&nbsp;<code>words<\/code>&nbsp;and concatenating them in&nbsp;<strong>any order<\/strong>. Each element can be selected&nbsp;<strong>at most once<\/strong>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>length<\/strong>&nbsp;of the longest palindrome that you can create<\/em>. If it is impossible to create any palindrome, return&nbsp;<code>0<\/code>.<\/p>\n\n\n\n<p>A&nbsp;<strong>palindrome<\/strong>&nbsp;is a string that reads the same forward and backward.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> words = [\"lc\",\"cl\",\"gg\"]\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> One longest palindrome is \"lc\" + \"gg\" + \"cl\" = \"lcggcl\", of length 6.\nNote that \"clgglc\" is another longest palindrome that can be created.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> words = [\"ab\",\"ty\",\"yt\",\"lc\",\"cl\",\"ab\"]\n<strong>Output:<\/strong> 8\n<strong>Explanation:<\/strong> One longest palindrome is \"ty\" + \"lc\" + \"cl\" + \"yt\" = \"tylcclyt\", of length 8.\nNote that \"lcyttycl\" is another longest palindrome that can be created.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> words = [\"cc\",\"ll\",\"xx\"]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> One longest palindrome is \"cc\", of length 2.\nNote that \"ll\" is another longest palindrome that can be created, and so is \"xx\".\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= words.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>words[i].length == 2<\/code><\/li><li><code>words[i]<\/code>&nbsp;consists of lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Match mirrored words<\/strong><\/h2>\n\n\n\n<p>For any pair of mirrored words, e.g. &#8216;ab&#8217; &lt;-&gt; &#8216;ba&#8217; or &#8216;aa&#8217;  &lt;-&gt; &#8216;aa&#8217;, we can extend the existing longest palindrome, ans += 4.<br>For any unpaired words with same letter, e.g. &#8216;cc&#8217;, we can only use one and put in the middle of the pladrome, ans += 2.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(26*26)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int longestPalindrome(vector<string>& words) {\n    int ans = 0;\n    int same = 0;\n    vector<vector<int>> m(26, vector<int>(26));          \n    for (const string& word : words) {      \n      const int a = word[0] - 'a';\n      const int b = word[1] - 'a';\n      if (m[b][a]) {\n        --m[b][a];\n        ans += 4;\n      } else {\n        ++m[a][b];\n      }\n    }\n    for (int i = 0; i < 26; ++i)\n      if (m[i][i]) {\n        ans += 2;\n        break;\n      }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array of strings\u00a0words. Each element of\u00a0words\u00a0consists of\u00a0two\u00a0lowercase English letters. Create the&nbsp;longest possible palindrome&nbsp;by selecting some elements from&nbsp;words&nbsp;and concatenating them in&nbsp;any order.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[82,177,95,4],"class_list":["post-9418","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-medium","tag-palindrome","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9418"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9418\/revisions"}],"predecessor-version":[{"id":9420,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9418\/revisions\/9420"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}