<p>Given a string <code>s</code>, rearrange the characters of <code>s</code> so that any two adjacent characters are not the same.</p>

<p>Return <em>any possible rearrangement of</em> <code>s</code> <em>or return</em> <code>&quot;&quot;</code> <em>if not possible</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "aab"
<strong>Output:</strong> "aba"
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "aaab"
<strong>Output:</strong> ""
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 500</code></li>
	<li><code>s</code> consists of lowercase English letters.</li>
</ul>

class Solution:
    def reorganizeString(self, s: str) -> str:
        import heapq
        from collections import Counter

        count = Counter(s)
        max_heap = [(-cnt, char) for char, cnt in count.items()]
        heapq.heapify(max_heap)

        prev_count, prev_char = 0, ''
        result = []

        while max_heap:
            cnt, char = heapq.heappop(max_heap)
            result.append(char)

            if prev_count < 0:
                heapq.heappush(max_heap, (prev_count, prev_char))

            prev_count, prev_char = cnt + 1, char

        result_str = ''.join(result)
        if len(result_str) != len(s):
            return ''

        return result_str

16500