{"id":356,"date":"2017-09-19T20:08:56","date_gmt":"2017-09-20T03:08:56","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=356"},"modified":"2018-07-10T18:25:31","modified_gmt":"2018-07-11T01:25:31","slug":"leetcode-409-longest-palindrome","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-409-longest-palindrome\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 409. Longest Palindrome"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 409. Longest Palindrome - \u5237\u9898\u627e\u5de5\u4f5c EP64\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/4GU7QvWffHQ?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/longest-palindrome\/description\/\">https:\/\/leetcode.com\/problems\/longest-palindrome\/description\/<\/a><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.<\/p>\n<p>This is case sensitive, for example\u00a0<code>\"Aa\"<\/code>\u00a0is not considered a palindrome here.<\/p>\n<p><b>Note:<\/b><br \/>\nAssume the length of given string will not exceed 1,010.<\/p>\n<p><b>Example:<\/b><\/p>\n<pre class=\"\">Input:\r\n\"abccccdd\"\r\n\r\nOutput:\r\n7\r\n\r\nExplanation:\r\nOne longest palindrome that can be built is \"dccaccd\", whose length is 7.<\/pre>\n<p><strong>Idea:<\/strong><\/p>\n<p>Greedy + Counting<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/409-ep64.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-359\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/409-ep64.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/409-ep64.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/409-ep64-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/409-ep64-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/409-ep64-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Time complexity: O(n)\r\n\/\/ Space complexity: O(128) = O(1)\r\n\/\/ Running time: 3 ms 9\/2017\r\nclass Solution {\r\npublic:\r\n    int longestPalindrome(const string&amp; s) {\r\n        vector&lt;int&gt; freqs(128, 0);\r\n        for (const char c : s)            \r\n            ++freqs[c];\r\n\r\n        int ans = 0;\r\n        int odd = 0;\r\n        for (const int freq : freqs) {            \r\n            \/\/ same as: ans += freq % 2 == 0 ? freq : freq - 1;\r\n            \/\/ same as: ans += freq \/ 2 * 2;\r\n            \/\/ same as: ans += ((freq &gt;&gt; 1) &lt;&lt; 1);            \r\n            \/\/ same as: ans += freq &amp; (INT_MAX - 1);\r\n            ans += freq &amp; (~1); \/\/ clear the last bit\r\n            odd |= freq &amp; 1;\r\n        }\r\n        \r\n        ans += odd;\r\n        \r\n        return ans;\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>Java<\/p>\n<pre class=\"lang:java decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 11 ms\r\nclass Solution {\r\n    public int longestPalindrome(String s) {        \r\n        int[] freqs = new int[128];\r\n        for (int i = 0; i &lt; s.length(); ++i)\r\n            ++freqs[s.charAt(i)];\r\n        \r\n        int ans = 0;\r\n        int odd = 0;\r\n        \r\n        for (int freq: freqs) {\r\n            ans += freq &amp; (~1);\r\n            odd |= freq &amp; 1;\r\n        }\r\n        \r\n        return ans + odd;\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Python<\/p>\n<pre class=\"lang:python decode:true  \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 39 ms\r\n\"\"\"\r\nclass Solution(object):\r\n    def longestPalindrome(self, s):\r\n        \"\"\"\r\n        :type s: str\r\n        :rtype: int\r\n        \"\"\"\r\n        freqs = [0] * 128\r\n        \r\n        for c in s:\r\n            freqs[ord(c)] += 1\r\n        \r\n        ans, odd = 0, 0\r\n        for freq in freqs:\r\n            ans += freq &amp; (~1)\r\n            odd |= freq &amp; 1\r\n        \r\n        return ans + odd\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode.com\/problems\/longest-palindrome\/description\/ Problem: Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51,70],"tags":[8,24],"class_list":["post-356","post","type-post","status-publish","format-standard","hentry","category-greedy","category-hashtable","tag-counting","tag-frequency","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/356","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=356"}],"version-history":[{"count":14,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/356\/revisions"}],"predecessor-version":[{"id":3052,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/356\/revisions\/3052"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}