<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) {
        vector<int> count(26, 0);
        priority_queue<pair<int, char>> pq;
        for (auto &c: s) {
            count[c - 'a']++;
        }
        for (int i = 0; i < count.size(); i++) {
            if (count[i] != 0) {
                pq.push({count[i], 'a' + i});
            }
        }
        string res = "";
        while (!pq.empty()) {
            auto p = pq.top();
            pq.pop();
            if (res.empty() || p.second != res.back()) {
                res += p.second;
                if (p.first > 1) {
                    pq.push({p.first - 1, p.second});
                }
            } else {
                if (pq.empty()) {
                    return "";
                }
                auto q = pq.top();
                pq.pop();
                res += q.second;
                if (q.first > 1) {
                    pq.push({q.first - 1, q.second});
                }
                pq.push(p);
            }
        }
        return res;
    }
};
8500