forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_358.java
43 lines (35 loc) · 1.46 KB
/
_358.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
public class _358 {
public static class Solution1 {
public String rearrangeString(String s, int k) {
Map<Character, Integer> count = new HashMap<>();
for (char c : s.toCharArray()) {
count.put(c, count.getOrDefault(c, 0) + 1);
}
PriorityQueue<Map.Entry<Character, Integer>> heap =
new PriorityQueue<>((a, b) -> b.getValue() - a.getValue());
heap.addAll(count.entrySet());
Queue<Map.Entry<Character, Integer>> waitQueue = new LinkedList<>();
StringBuilder stringBuilder = new StringBuilder();
while (!heap.isEmpty()) {
Map.Entry<Character, Integer> entry = heap.poll();
stringBuilder.append(entry.getKey());
entry.setValue(entry.getValue() - 1);
waitQueue.offer(entry);
if (waitQueue.size() < k) {
continue; //there's only k-1 chars in the waitHeap, not full yet
}
Map.Entry<Character, Integer> front = waitQueue.poll();
if (front.getValue() > 0) {
heap.offer(front);
}
}
return stringBuilder.length() == s.length() ? stringBuilder.toString() : "";
}
}
}