-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestDiverseString.java
More file actions
40 lines (29 loc) · 1.01 KB
/
longestDiverseString.java
File metadata and controls
40 lines (29 loc) · 1.01 KB
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
class Solution {
public String longestDiverseString(int a, int b, int c) {
PriorityQueue<int[]> pq = new PriorityQueue<>(
(x, y) -> y[1] - x[1]
);
if (a > 0) pq.add(new int[]{'a', a});
if (b > 0) pq.add(new int[]{'b', b});
if (c > 0) pq.add(new int[]{'c', c});
StringBuilder res = new StringBuilder();
while (!pq.isEmpty()) {
int[] first = pq.poll();
int len = res.length();
if (len >= 2 && res.charAt(len - 1) == first[0] && res.charAt(len - 2) == first[0]) {
if (pq.isEmpty()) break;
int[] second = pq.poll();
res.append((char) second[0]);
second[1]--;
if (second[1] > 0) pq.add(second);
pq.add(first);
}
else {
res.append((char) first[0]);
first[1]--;
if (first[1] > 0) pq.add(first);
}
}
return res.toString();
}
}