leetcode-767. 重构字符串

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
44
45
46
47
48
49
50
51
class Solution {
public String reorganizeString(String S) {
String ans = "";
char[] ch = S.toCharArray();
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < ch.length; i++) {
map.put(ch[i], map.getOrDefault(ch[i], 0) + 1);
}

PriorityQueue<Character> queue = new PriorityQueue<>((o1, o2) -> map.get(o2).compareTo(map.get(o1)));

for(Map.Entry<Character, Integer> entry : map.entrySet()){
// 入队
queue.offer(entry.getKey());
}
Character pre = null;
while (!queue.isEmpty()){
Character c1 = queue.poll();
Character c2 = queue.poll();
if (c1 == pre && c2 == null) {
return "";
}

if (c1 != pre){
Integer c1Count = map.get(c1);
map.put(c1, c1Count - 1);
if (c1Count > 1) {
// 再次入队
queue.offer(c1);
}
if(c2 != null){
queue.offer(c2);
}
pre = c1;
ans += c1;
}else {
// 取c2
Integer c2Count = map.get(c2);
map.put(c2, c2Count - 1);
if (c2Count > 1) {
// 再次入队
queue.offer(c2);
}
queue.offer(c1);
pre = c2;
ans += c2;
}
}
return ans;
}
}