<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: 
static bool cmp(pair<char,int> &p1 ,pair<char,int>&p2){
        if(p1.second != p2.second){
            return p2.second < p1.second;
        }
        return p1.first <  p2.first;
    }
public:
    string reorganizeString(string s) {
        
        int n = s.size();

        map<char,int>mp;

        for(int i=0;i<n;i++){
            mp[s[i]]++;
        }
        vector<pair<char,int> >v;

        for(auto it : mp){
            v.push_back({it.first,it.second});
        }

        sort(v.begin(),v.end(),cmp);

        if(v[0].second > (n+1)/2){return "";}

        char arr[n];
        int ind = 0;

        for(int i=0;i<v.size();i++){
            int c = v[i].first;
            int cnt = v[i].second;

            while(cnt--){
                if(ind>=n){ind = 1;}
                arr[ind] = c;
                ind+=2;
            }
        }

        string ans = "";
        for(int i=0;i<n;i++){
            ans+=arr[i];
        }
        return ans;
    }
};
8900