<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) {
        int n = s.size();

        unordered_map<char, int> map;
        // chalo map ko fill krte hai
        for (char c : s) {
            map[c]++;
        }

        // find the most frequent char
        char max_freq_char;
        int max_freq = 0;
        for (auto element : map) {
            // if max freq int min se jyada hai to usko uske equal krlo
            if (element.second > max_freq) {
                max_freq = element.second;
                max_freq_char = element.first;
            }
        }
        // max freq char ko place kr rhe hai
        int index = 0;
        while (max_freq > 0 && index < n) {
            s[index] = max_freq_char;
            max_freq--;
            index += 2;
        }
        // if yha tk me sare a jo ki most freq char place kr paya hunga then max
        // freq=0 if nii kr pae hai to empty return krdo
        if (max_freq != 0) {
            return "";
        }
        map[max_freq_char] = 0;
        // use of priority quene
 
        // lets place the rest of char

        for (auto element : map) {
            while (element.second > 0) {
                index = index >= n? 1: index; // if index greater then size ho gya to usko 1 // kr dege vrtna usko index hi rhne dege
                s[index] = element.first;
                element.second--;
                index += 2;
            }
        }
        return s;
    }
};
8100