public String rearrangeString(String s, int k) {
int[] count = new int[256];
for (char c : s.toCharArray()) {
PriorityQueue<CharInfo> maxHeap = new PriorityQueue<>();
for (int i = 0; i < 256; i++) {
maxHeap.add(new CharInfo((char)i, count[i]));
Queue<CharInfo> buffer = new LinkedList<>();
StringBuilder res = new StringBuilder();
while (!maxHeap.isEmpty()) {
CharInfo curChar = maxHeap.remove();
if (buffer.size() == k) {
curChar = buffer.remove();
return res.length() == s.length() ? res.toString() : "";
class CharInfo implements Comparable<CharInfo> {
public CharInfo(char val, int freq) {
public int compareTo(CharInfo that) {
return that.freq - this.freq;