{"id":6581,"date":"2020-04-05T01:23:13","date_gmt":"2020-04-05T08:23:13","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6581"},"modified":"2020-04-05T01:33:00","modified_gmt":"2020-04-05T08:33:00","slug":"leetcode-1405-longest-happy-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1405-longest-happy-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1405. Longest Happy String"},"content":{"rendered":"\n<p>A string is called&nbsp;<em>happy<\/em>&nbsp;if it does&nbsp;not have any of the strings&nbsp;<code>'aaa'<\/code>,&nbsp;<code>'bbb'<\/code>&nbsp;or&nbsp;<code>'ccc'<\/code>&nbsp;as a substring.<\/p>\n\n\n\n<p>Given three integers&nbsp;<code>a<\/code>,&nbsp;<code>b<\/code>&nbsp;and&nbsp;<code>c<\/code>, return&nbsp;<strong>any<\/strong>&nbsp;string&nbsp;<code>s<\/code>,&nbsp;which satisfies following conditions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>s<\/code>&nbsp;is&nbsp;<em>happy&nbsp;<\/em>and longest possible.<\/li><li><code>s<\/code>&nbsp;contains&nbsp;<strong>at most<\/strong>&nbsp;<code>a<\/code>&nbsp;occurrences of the letter&nbsp;<code>'a'<\/code>,&nbsp;<strong>at most<\/strong>&nbsp;<code>b<\/code>&nbsp;occurrences of the letter&nbsp;<code>'b'<\/code>&nbsp;and&nbsp;<strong>at most<\/strong>&nbsp;<code>c<\/code>&nbsp;occurrences of the letter&nbsp;<code>'c'<\/code>.<\/li><li><code>s&nbsp;<\/code>will only contain&nbsp;<code>'a'<\/code>,&nbsp;<code>'b'<\/code>&nbsp;and&nbsp;<code>'c'<\/code>&nbsp;letters.<\/li><\/ul>\n\n\n\n<p>If there is no such string&nbsp;<code>s<\/code>&nbsp;return the empty string&nbsp;<code>\"\"<\/code>.<\/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> a = 1, b = 1, c = 7\n<strong>Output:<\/strong> \"ccaccbcc\"\n<strong>Explanation:<\/strong> \"ccbccacc\" would also be a correct answer.\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> a = 2, b = 2, c = 1\n<strong>Output:<\/strong> \"aabbc\"\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> a = 7, b = 1, c = 0\n<strong>Output:<\/strong> \"aabaa\"\n<strong>Explanation:<\/strong> It's the only correct answer in this case.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= a, b, c &lt;= 100<\/code><\/li><li><code>a + b + c &gt; 0<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Put the char with highest frequency first if its consecutive length of that char is &lt; 2 <br>or put one char if any of other two chars has consecutive length of 2.<\/p>\n\n\n\n<p>increase the consecutive length of itself and reset that for other two chars.<\/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  string longestDiverseString(int a, int b, int c) {\n    const int total = a + b + c;        \n    vector<int> f{a, b, c};\n    vector<int> l{0, 0, 0};\n    string ans;\n    for (int _ = 0; _ < total; ++_)\n      for (int i = 0; i < 3; ++i) {\n        const int j = (i + 1) % 3;\n        const int k = (i + 2) % 3;        \n        if ((f[i] >= f[j] && f[i] >= f[k] && l[i] != 2) \n            || (f[i] > 0 && (l[j] == 2 || l[k] == 2))) {\n          ans += 'a' + i;\n          ++l[i];\n          --f[i];\n          l[j] = l[k] = 0;\n          break;\n      }    \n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A string is called&nbsp;happy&nbsp;if it does&nbsp;not have any of the strings&nbsp;&#8216;aaa&#8217;,&nbsp;&#8216;bbb&#8217;&nbsp;or&nbsp;&#8216;ccc&#8217;&nbsp;as a substring. Given three integers&nbsp;a,&nbsp;b&nbsp;and&nbsp;c, return&nbsp;any&nbsp;string&nbsp;s,&nbsp;which satisfies following conditions: s&nbsp;is&nbsp;happy&nbsp;and longest possible. s&nbsp;contains&nbsp;at most&nbsp;a&nbsp;occurrences&#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],"tags":[578,88,177,4],"class_list":["post-6581","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-construction","tag-greedy","tag-medium","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6581","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=6581"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6581\/revisions"}],"predecessor-version":[{"id":6586,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6581\/revisions\/6586"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}