{"id":6565,"date":"2020-04-04T20:11:03","date_gmt":"2020-04-05T03:11:03","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6565"},"modified":"2020-04-04T20:28:02","modified_gmt":"2020-04-05T03:28:02","slug":"leetcode-1400-construct-k-palindrome-strings","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1400-construct-k-palindrome-strings\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1400. Construct K Palindrome Strings"},"content":{"rendered":"\n<p>Given a string&nbsp;<code>s<\/code>&nbsp;and an integer&nbsp;<code>k<\/code>. You should construct&nbsp;<code>k<\/code>&nbsp;non-empty&nbsp;<strong>palindrome<\/strong>&nbsp;strings using&nbsp;<strong>all the characters<\/strong>&nbsp;in&nbsp;<code>s<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em><strong>True<\/strong><\/em>&nbsp;if you can use all the characters in&nbsp;<code>s<\/code>&nbsp;to construct&nbsp;<code>k<\/code>&nbsp;palindrome strings or&nbsp;<em><strong>False<\/strong><\/em>&nbsp;otherwise.<\/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> s = \"annabelle\", k = 2\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> You can construct two palindromes using all characters in s.\nSome possible constructions \"anna\" + \"elble\", \"anbna\" + \"elle\", \"anellena\" + \"b\"\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> s = \"leetcode\", k = 3\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> It is impossible to construct 3 palindromes using all the characters of s.\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> s = \"true\", k = 4\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> The only possible solution is to put each character in a separate string.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"yzyzyzyzyzyzyzy\", k = 2\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"cr\", k = 7\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> We don't have enough characters in s to construct 7 palindromes.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 10^5<\/code><\/li><li>All characters in&nbsp;<code>s<\/code>&nbsp;are lower-case English letters.<\/li><li><code>1 &lt;= k &lt;= 10^5<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: HashTable<\/strong><\/h2>\n\n\n\n<p>Compute the frequency of each characters.<\/p>\n\n\n\n<p>Count the # of characters with odd frequency |odd|, each palindrome can consume at most one char with odd frequency. thus k must >= |odd|.<br>ans = k &lt;= len(s) and k >= odd<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/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  bool canConstruct(string s, int k) {    \n    if (k > s.length()) return false;\n    vector<int> f(26);\n    for (char c : s) f[c - 'a'] ^= 1;\n    int odd = accumulate(begin(f), end(f), 0);\n    return k >= odd;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s&nbsp;and an integer&nbsp;k. You should construct&nbsp;k&nbsp;non-empty&nbsp;palindrome&nbsp;strings using&nbsp;all the characters&nbsp;in&nbsp;s. Return&nbsp;True&nbsp;if you can use all the characters in&nbsp;s&nbsp;to construct&nbsp;k&nbsp;palindrome strings or&nbsp;False&nbsp;otherwise. Example 1: Input:&#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":[],"class_list":["post-6565","post","type-post","status-publish","format-standard","hentry","category-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6565","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=6565"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6565\/revisions"}],"predecessor-version":[{"id":6568,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6565\/revisions\/6568"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}