<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 hash[26] = {0};
        for(int i = 0; i < s.size(); i++) {
            hash[s[i] - 'a']++;
        }

        char max_freq_char;
        int max_freq = INT_MIN;
        
        // Find the character with the maximum frequency
        for(int i = 0; i < 26; i++) { // Loop over all alphabet characters
            if(hash[i] > max_freq) {
                max_freq = hash[i];
                max_freq_char = i + 'a';
            }
        }
        // Place the maximum frequency character at even indexes
        int index = 0;
        while(max_freq > 0 && index < s.size()) {
            s[index] = max_freq_char;
            max_freq--;
            index += 2;
        }
        if(max_freq!=0){
            return "";
        }

        // Set its frequency to 0 since we have used it
        hash[max_freq_char - 'a'] = 0;

        // Place the remaining characters
        for(int i = 0; i < 26; i++) {
            while(hash[i] > 0) {
                index = index >= s.size() ? 1 : index;  // Move to odd index if even indexes are filled
                s[index] = i + 'a';
                hash[i]--;
                index += 2;
            }
        }

        return s;
    }
};

7600