private static Character[] rearrange(String str, int k) {
        PriorityQueue<CharCount> queue = new PriorityQueue<>();
        Map<Character, Integer> counts = new HashMap<>();
        for (char c : str.toCharArray()) {
            if (!counts.containsKey(c)) {
                counts.put(c, 1);
            } else {
                counts.put(c, counts.get(c) + 1);
            }
        }
        counts.forEach((c, count) -> {
            queue.add(new CharCount(c, count));
        });
        Character[] chars = new Character[str.length()];
        while (!queue.isEmpty()) {
            CharCount cc = queue.remove();
            int idx = 0;
            while (chars[idx] != null) {
                idx++;
            }
            for (int i = idx, j = 0; i < chars.length && j < cc.count; i += k, j++) {
                if (chars[i] != null) {
                    return new Character[]{};
                }
                chars[i] = cc.character;
            }
        }
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == null) {
                return new Character[]{};
            }
        }
        return chars;
    }