<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) {
        priority_queue<pair<int, char>, vector<pair<int, char>>> maxheap; 
        string res = "";
        unordered_map<char, int> letterMap; // can be optimized with int[] array
        
        for (char c : s) {
            letterMap[c]++;
        }
        for (auto &x : letterMap) {
            maxheap.push(make_pair(x.second, x.first));
        }
        
        while (!maxheap.empty()) {
            char firstChar = maxheap.top().second;
            int firstCharFreq = maxheap.top().first;
            maxheap.pop();
            if (!res.empty() && res.back() == firstChar)
                return "";
            
            res += firstChar;
            firstCharFreq--;
            
            if (!maxheap.empty()) {
                char secondChar = maxheap.top().second;
                int secondCharFreq = maxheap.top().first;
                maxheap.pop();
                //if (!res.empty() && res.back() == secondLetter) return "";
                res += secondChar;
                secondCharFreq--;
                if (secondCharFreq > 0)
                    maxheap.push(make_pair(secondCharFreq, secondChar));
            }
            
            if (firstCharFreq > 0)
                maxheap.push(make_pair(firstCharFreq, firstChar));
            
        }
        
        return res;
    }
};
8700