<p>Given an array of digits <code>digits</code>, return <em>the largest multiple of <strong>three</strong> that can be formed by concatenating some of the given digits in <strong>any order</strong></em>. If there is no answer return an empty string.</p>

<p>Since the answer may not fit in an integer data type, return the answer as a string. Note that the returning answer must not contain unnecessary leading zeros.</p>

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

<pre>
<strong>Input:</strong> digits = [8,1,9]
<strong>Output:</strong> &quot;981&quot;
</pre>

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

<pre>
<strong>Input:</strong> digits = [8,6,7,1,0]
<strong>Output:</strong> &quot;8760&quot;
</pre>

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

<pre>
<strong>Input:</strong> digits = [1]
<strong>Output:</strong> &quot;&quot;
</pre>

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

<ul>
	<li><code>1 &lt;= digits.length &lt;= 10<sup>4</sup></code></li>
	<li><code>0 &lt;= digits[i] &lt;= 9</code></li>
</ul>

class Solution:
    def largestMultipleOfThree(self, digits: List[int]) -> str:
        amounts = [{0: 0, 3: 0, 6: 0, 9: 0}, {1: 0, 4: 0, 7: 0}, {2: 0, 5: 0, 8: 0}]
        for i in digits:
            amounts[i % 3][i] += 1
        total = 0
        for i in amounts[1]:
            total += amounts[1][i]
        for i in amounts[2]:
            total -= amounts[2][i]
        total = total % 3
        # total = 1 -> remove 1 from list1
        # total = 2 -> remove 1 from list2
        if total == 1:
            rem = 1
            index = 1
            while rem > 0 and index < 10:
                use = min(amounts[1][index], rem)
                amounts[1][index] -= use
                rem -= use
                index += 3
            if rem == 1:
                index = 2
                rem = 2
                while rem > 0 and index < 10:
                    use = min(amounts[2][index], rem)
                    amounts[2][index] -= use
                    rem -= use
                    index += 3
        elif total == 2:
            rem = 1
            index = 2
            while rem > 0 and index < 10:
                use = min(amounts[2][index], rem)
                amounts[2][index] -= use
                rem -= use
                index += 3
            if rem == 1:
                index = 1
                rem = 2
                while rem > 0 and index < 10:
                    use = min(amounts[1][index], rem)
                    amounts[1][index] -= use
                    rem -= use
                    index += 3
        script = amounts[0][9]*"9"+amounts[2][8]*"8"+amounts[1][7]*"7"+amounts[0][6]*"6"+amounts[2][5]*"5"+amounts[1][4]*"4"+amounts[0][3]*"3"+amounts[2][2]*"2"+amounts[1][1]*"1"+amounts[0][0]*"0"
        if len(script) > 0 and script[0] == "0":
            return "0"
        return script
        
127