<p>Given a string <code>s</code>, sort it in <strong>decreasing order</strong> based on the <strong>frequency</strong> of the characters. The <strong>frequency</strong> of a character is the number of times it appears in the string.</p>

<p>Return <em>the sorted string</em>. If there are multiple answers, return <em>any of them</em>.</p>

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

<pre>
<strong>Input:</strong> s = &quot;tree&quot;
<strong>Output:</strong> &quot;eert&quot;
<strong>Explanation:</strong> &#39;e&#39; appears twice while &#39;r&#39; and &#39;t&#39; both appear once.
So &#39;e&#39; must appear before both &#39;r&#39; and &#39;t&#39;. Therefore &quot;eetr&quot; is also a valid answer.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;cccaaa&quot;
<strong>Output:</strong> &quot;aaaccc&quot;
<strong>Explanation:</strong> Both &#39;c&#39; and &#39;a&#39; appear three times, so both &quot;cccaaa&quot; and &quot;aaaccc&quot; are valid answers.
Note that &quot;cacaca&quot; is incorrect, as the same characters must be together.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;Aabb&quot;
<strong>Output:</strong> &quot;bbAa&quot;
<strong>Explanation:</strong> &quot;bbaA&quot; is also a valid answer, but &quot;Aabb&quot; is incorrect.
Note that &#39;A&#39; and &#39;a&#39; are treated as two different characters.
</pre>

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

<ul>
	<li><code>1 &lt;= s.length &lt;= 5 * 10<sup>5</sup></code></li>
	<li><code>s</code> consists of uppercase and lowercase English letters and digits.</li>
</ul>

from collections import Counter
from functools import cmp_to_key

class Solution:
    # TC: n + n * log n = O(n * log n); SC: O(n)
    def solve_by_counter(self, line: str) -> str:
        counts = Counter(line)
        counts = counts.most_common(len(counts))
        result = ''
        
        for char, count in counts:
            result += char * count
        
        return result
    

    # TC: n + n * log n + n + n * n log n + n = O(n * log n); SC: n + n = O(n)
    def solve_by_arr_sort(self, line: str) -> str:
        chars = sorted(list(line))
        groups = []        
        group = []

        for char in chars:
            if not group or group[-1] == char:
                group += char
            else:
                groups.append(
                    ''.join(group)
                )
                group = char
        
        groups.append(
            ''.join(group)
        )
        groups.sort(key=cmp_to_key(
            lambda a, b: len(b)-len(a)
        ))

        return ''.join(groups)

    # TC: n + n + n + n + n + n = O(n); SC: O(n);
    def solve_by_bucket_sort(self, line: str) -> str:
        counts = {}

        for char in line:
            counts[char] = counts.get(char, 0) + 1

        max_count = max(counts.values())
        buckets = [
            [] for _ in range(max_count + 1)
        ]
        result = []

        for char, count in counts.items():
            buckets[count].append(
                char * count
            )
        
        for i in range(len(buckets) - 1, 0, -1):
            result.append(
                ''.join(buckets[i])
            )
        
        return ''.join(result)


    def frequencySort(self, line: str) -> str:
        # return self.solve_by_counter(line)
        # return self.solve_by_arr_sort(line)
        return self.solve_by_bucket_sort(line)
21772