<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 {
public:
    string reorganizeString(string s) {
        unordered_map<char, int> charFrequencies; 
        for (char c : s) {
            charFrequencies[c]++; 
        }

        // max heap to store {frequency, character} pairs
        priority_queue<pair<int, char>> maxHeap; 
        for (auto& entry : charFrequencies) {
            maxHeap.push({entry.second, entry.first});
        }

        string reorganizedString;
        pair<int, char> prev = {-1, '#'}; // track the previous character to avoid repetition

        while (!maxHeap.empty()) {
            auto [frequency, character] = maxHeap.top();
            maxHeap.pop();

            reorganizedString += character; // append the character to the result

            // if the previous character's frequency is still > 0, add it back to the heap
            if (prev.first > 0) { 
                maxHeap.push(prev);
            }

            frequency--; // decrement the frequency of the current character
            prev = {frequency, character}; // update the previous character
        }

        // if we couldn't form a valid string (due to adjacent repetitions), return an empty string
        return reorganizedString.length() == s.length() ? reorganizedString : ""; 
    }
};
9000