<p>A string <code>s</code> is called <strong>happy</strong> if it satisfies the following conditions:</p>

<ul>
	<li><code>s</code> only contains the letters <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</li>
	<li><code>s</code> does not contain any of <code>&quot;aaa&quot;</code>, <code>&quot;bbb&quot;</code>, or <code>&quot;ccc&quot;</code> as a substring.</li>
	<li><code>s</code> contains <strong>at most</strong> <code>a</code> occurrences of the letter <code>&#39;a&#39;</code>.</li>
	<li><code>s</code> contains <strong>at most</strong> <code>b</code> occurrences of the letter <code>&#39;b&#39;</code>.</li>
	<li><code>s</code> contains <strong>at most</strong> <code>c</code> occurrences of the letter <code>&#39;c&#39;</code>.</li>
</ul>

<p>Given three integers <code>a</code>, <code>b</code>, and <code>c</code>, return <em>the <strong>longest possible happy </strong>string</em>. If there are multiple longest happy strings, return <em>any of them</em>. If there is no such string, return <em>the empty string </em><code>&quot;&quot;</code>.</p>

<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> a = 1, b = 1, c = 7
<strong>Output:</strong> &quot;ccaccbcc&quot;
<strong>Explanation:</strong> &quot;ccbccacc&quot; would also be a correct answer.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> a = 7, b = 1, c = 0
<strong>Output:</strong> &quot;aabaa&quot;
<strong>Explanation:</strong> It is the only correct answer in this case.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= a, b, c &lt;= 100</code></li>
	<li><code>a + b + c &gt; 0</code></li>
</ul>

class Solution {
public:
    string longestDiverseString(int a, int b, int c) {
        stack<int> str;
        string ans = "";
        while (min({a,b,c}) > 0) {
            str.push(1);
            str.push(2);
            str.push(3);
            a--;
            b--;
            c--;
        }
        while (min({a,b}) > 0) {
            str.push(1);
            str.push(2);
            a--;
            b--;
        }
        while (min({a,c}) > 0) {
            str.push(1);
            str.push(3);
            a--;
            c--;
        }
        while (min({b,c}) > 0) {
            str.push(2);
            str.push(3);
            b--;
            c--;
        }
        stack<int> str2;
        while (!str.empty() && a-->0) {
            if (str.top() != 1) {
                str2.push(1);
                if (a-- > 0) str2.push(1);
                str2.push(str.top());
                str.pop();
            } else {
                str2.push(1);
                str2.push(1);
                str.pop();
                if (!str.empty()) {
                    str2.push(str.top());
                    str.pop();
                }
            }
        }
        while (!str.empty() && b-->0) {
            if (str.top() != 2) {
                str2.push(2);
                if (b-- > 0) str2.push(2);
                str2.push(str.top());
                str.pop();
            } else {
                str2.push(2);
                str2.push(2);
                str.pop();
                if (!str.empty()) {
                    str2.push(str.top());
                    str.pop();
                }
            }
        }
        while (!str.empty() && c-->0) {
            if (str.top() != 3) {
                str2.push(3);
                if (c-- > 0) str2.push(3);
                str2.push(str.top());
                str.pop();
            } else {
                str2.push(3);
                str2.push(3);
                str.pop();
                if (!str.empty()) {
                    str2.push(str.top());
                    str.pop();
                }
            }
        }
        while (!str.empty()) {
            str2.push(str.top());
            str.pop();
        }
        if (a-- > 0 && (str2.empty() || str2.top() != 1)) {
            str2.push(1);
            if (a-- > 0) str2.push(1);
        }
        if (b-- > 0 && (str2.empty() || str2.top() != 2)) {
            str2.push(2);
            if (b-- > 0) str2.push(2);
        }
        if (c-- > 0 && (str2.empty() || str2.top() != 3)) {
            str2.push(3);
            if (c-- > 0) str2.push(3);
        }
        while (!str2.empty()) {
            if (str2.top() == 1) ans += 'a';
            else if (str2.top() == 2) ans += 'b';
            else ans += 'c';
            str2.pop();
        }
        return ans;
    }
};
8200