{"id":6412,"date":"2020-03-08T11:19:38","date_gmt":"2020-03-08T18:19:38","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6412"},"modified":"2020-03-08T11:20:58","modified_gmt":"2020-03-08T18:20:58","slug":"leetcode-1374-generate-a-string-with-characters-that-have-odd-counts","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1374-generate-a-string-with-characters-that-have-odd-counts\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1374. Generate a String With Characters That Have Odd Counts"},"content":{"rendered":"\n<p>Given an&nbsp;integer&nbsp;<code>n<\/code>,&nbsp;<em>return a string with&nbsp;<code>n<\/code>&nbsp;characters such that each character in such string occurs&nbsp;<strong>an odd number of times<\/strong><\/em>.<\/p>\n\n\n\n<p>The returned string must contain only lowercase English letters. If there are multiples valid strings, return&nbsp;<strong>any<\/strong>&nbsp;of them. &nbsp;<\/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> n = 4\n<strong>Output:<\/strong> \"pppz\"\n<strong>Explanation:<\/strong> \"pppz\" is a valid string since the character 'p' occurs three times and the character 'z' occurs once. Note that there are many other valid strings such as \"ohhh\" and \"love\".\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> n = 2\n<strong>Output:<\/strong> \"xy\"\n<strong>Explanation:<\/strong> \"xy\" is a valid string since the characters 'x' and 'y' occur once. Note that there are many other valid strings such as \"ag\" and \"ur\".\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> n = 7\n<strong>Output:<\/strong> \"holasss\"\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 500<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>if n is odd, return n &#8216;a&#8217;s.<br>otherwise, return n -1 &#8216;a&#8217;s and 1 &#8216;b&#8217;<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n) or 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++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  string generateTheString(int n) {    \n    return n &amp; 1 ? string(n, 'a') : string(n - 1, 'a') + \"b\";\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\"># Author: Huahua\nclass Solution:\n  def generateTheString(self, n: int) -&gt; str:\n    ans = ['a'] * n if n % 2 == 1 else ['a'] * (n - 1) + ['b']\n    return ''.join(ans)\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an&nbsp;integer&nbsp;n,&nbsp;return a string with&nbsp;n&nbsp;characters such that each character in such string occurs&nbsp;an odd number of times. The returned string must contain only lowercase English&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[222,376,4],"class_list":["post-6412","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-on","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6412","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=6412"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6412\/revisions"}],"predecessor-version":[{"id":6414,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6412\/revisions\/6414"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}