{"id":4709,"date":"2019-01-27T10:42:07","date_gmt":"2019-01-27T18:42:07","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4709"},"modified":"2019-01-27T10:53:29","modified_gmt":"2019-01-27T18:53:29","slug":"leetcode-984-string-without-aaa-or-bbb","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-984-string-without-aaa-or-bbb\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 984. String Without AAA or BBB"},"content":{"rendered":"\n<p>Given two integers&nbsp;<code>A<\/code>&nbsp;and&nbsp;<code>B<\/code>, return&nbsp;<strong>any<\/strong>&nbsp;string&nbsp;<code>S<\/code>&nbsp;such that:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>S<\/code>&nbsp;has length&nbsp;<code>A + B<\/code>&nbsp;and contains exactly&nbsp;<code>A<\/code>&nbsp;<code>'a'<\/code>&nbsp;letters, and exactly&nbsp;<code>B<\/code>&nbsp;<code>'b'<\/code>&nbsp;letters;<\/li><li>The substring&nbsp;<code>'aaa'<\/code>&nbsp;does not occur in&nbsp;<code>S<\/code>;<\/li><li>The substring&nbsp;<code>'bbb'<\/code>&nbsp;does not occur in&nbsp;<code>S<\/code>.<\/li><\/ul>\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 = 2\n<strong>Output: <\/strong>\"abb\"\n<strong>Explanation:<\/strong> \"abb\", \"bab\" and \"bba\" are all correct answers.\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 = 4, B = 1\n<strong>Output: <\/strong>\"aabaa\"<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>0 &lt;= A &lt;= 100<\/code><\/li><li><code>0 &lt;= B &lt;= 100<\/code><\/li><li>It is guaranteed such an&nbsp;<code>S<\/code>&nbsp;exists for the given&nbsp;<code>A<\/code>&nbsp;and&nbsp;<code>B<\/code>.<\/li><\/ol>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">class Solution {\npublic:\n  string strWithout3a3b(int A, int B) {\n    char a = 'a';\n    char b = 'b';\n    if (B > A) {\n      swap(A, B);\n      swap(a, b);\n    }\n    string ans;\n    while (A || B) {\n      if (A > 0) { ans += a; --A; }\n      if (A > B) { ans += a; --A; }\n      if (B > 0) { ans += b; --B; }\n    }\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++ \/ overload<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">class Solution {\npublic:\n  string strWithout3a3b(int A, int B, char a = 'a', char b = 'b') {\n    if (B > A) return strWithout3a3b(B, A, b, a);\n    string ans;\n    while (A || B) {\n      if (A > 0) { ans += a; --A; }\n      if (A > B) { ans += a; --A; }\n      if (B > 0) { ans += b; --B; }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given two integers&nbsp;A&nbsp;and&nbsp;B, return&nbsp;any&nbsp;string&nbsp;S&nbsp;such that: S&nbsp;has length&nbsp;A + B&nbsp;and contains exactly&nbsp;A&nbsp;&#8216;a&#8217;&nbsp;letters, and exactly&nbsp;B&nbsp;&#8216;b&#8217;&nbsp;letters; The substring&nbsp;&#8216;aaa&#8217;&nbsp;does not occur in&nbsp;S; The substring&nbsp;&#8216;bbb&#8217;&nbsp;does not occur in&nbsp;S. Example 1:&#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":[457,88,4],"class_list":["post-4709","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-construct","tag-greedy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4709","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=4709"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4709\/revisions"}],"predecessor-version":[{"id":4718,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4709\/revisions\/4718"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}