<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:
    struct CustomSort {
        bool operator()(auto a, auto b) {
            return a.second <= b.second;
        }
    };
    string reorganizeString(string s) {
        map<char,int> countMap;

        for(auto ch: s){
            countMap[ch]++;
        }

        priority_queue<pair<char,int>, vector<pair<char, int>>, CustomSort> pq;

        for(auto [key, val]: countMap) {
            pq.push({key, val});
        }

        pair<char, int> prev = {'#', -1};

        string res = "";

        while(!pq.empty()) {
            auto [ch, count] = pq.top();
            pq.pop();

            if(prev.second > 0) {
                pq.push(prev);
            }

            res += ch;
            prev = {ch, count - 1};
        }

        return res.size() == s.size() ? res : "";
    }
};
9800