<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)
//     {
//         // the idea is to create a hash table from chars to their counts.
//         // Iterate over them with the algo alternate min, max: if we have a = 2, b= 3, c = 4.
//         // output will be acacbcbcb.
//         int n = s.size() ;
//         std :: map<char, int> M;
//         for(char c : s)
//         {
//             M[c]++;
//         } 
//         vector<pair<char, int>> V;
//         for(auto &pair : M)
//         {
//             V.push_back(pair) ;
//         }
//         int i = 0;
//         int j = V.size()-1 ;
//         string fstr = "" ;
//         while(i<j)
//         {
//             fstr += V[i].first ;
//             fstr += V[j].first ;
//             V[i].second--;
//             V[j].second--;
//             if(!V[i].second) {i++;}
//             if(!V[j].second) {j--;}
//         }
//         if(i>j)
//         {
//             return fstr;
//         }
//         else
//         {
//             if (V[i].second>=2) { return std :: string("") ; }
//             if (V[i].first == fstr[n-2]) {return std:: string(""); }
//             return fstr+V[i].first ;  
//         }
//     }
// };

class Solution {
public:
    string reorganizeString(string s) {
        string ans=""; 
        map<char,int>map;
        for(int i=0;i<s.length();i++){
            map[s[i]]++;
        }

        vector< pair <int,char> > vect;
        for(auto i : map){
          vect.push_back(make_pair(i.second,i.first));
        }
 
    
         
        sort(vect.begin(),vect.end(),greater<pair <int,char>>());
        ans+=vect[0].second;
        if( vect[0].first>0){
             vect[0].first--;
        }
       for(int i=1;i<s.length();i++){
           sort(vect.begin(),vect.end(),greater<pair <int,char>>());
           if(ans[i-1]!=vect[0].second  ){
              ans.push_back(vect[0].second);
              vect[0].first--;
           }
           else{
               if(vect.size()==1  || vect[1].first<=0 ){
                   return "";
               }
               else{
                   ans.push_back(vect[1].second);
                   vect[1].first--;
              }
             
           }
            
       }

       return ans;
        
    }
};
9900