All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0701_0800.s0767_reorganize_string.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g0701_0800.s0767_reorganize_string;

// #Medium #String #Hash_Table #Sorting #Greedy #Heap_Priority_Queue #Counting
// #2022_03_26_Time_1_ms_(94.60%)_Space_42.4_MB_(42.02%)

/**
 * 767 - Reorganize String\.
 *
 * Medium
 *
 * Given a string `s`, rearrange the characters of `s` so that any two adjacent characters are not the same.
 *
 * Return _any possible rearrangement of_ `s` _or return_ `""` _if not possible_.
 *
 * **Example 1:**
 *
 * **Input:** s = "aab"
 *
 * **Output:** "aba"
 *
 * **Example 2:**
 *
 * **Input:** s = "aaab"
 *
 * **Output:** ""
 *
 * **Constraints:**
 *
 * *   `1 <= s.length <= 500`
 * *   `s` consists of lowercase English letters.
**/
public class Solution {
    public String reorganizeString(String s) {
        int[] hash = new int[26];
        for (int i = 0; i < s.length(); i++) {
            hash[s.charAt(i) - 'a']++;
        }
        int max = 0;
        int letter = 0;
        for (int i = 0; i < hash.length; i++) {
            if (hash[i] > max) {
                max = hash[i];
                letter = i;
            }
        }
        if (max > (s.length() + 1) / 2) {
            return "";
        }
        char[] res = new char[s.length()];
        int idx = 0;
        while (hash[letter] > 0) {
            res[idx] = (char) (letter + 'a');
            idx += 2;
            hash[letter]--;
        }
        for (int i = 0; i < hash.length; i++) {
            while (hash[i] > 0) {
                if (idx >= res.length) {
                    idx = 1;
                }
                res[idx] = (char) (i + 'a');
                idx += 2;
                hash[i]--;
            }
        }
        return String.valueOf(res);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy