-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetcode_1405.java
39 lines (30 loc) · 1.29 KB
/
Leetcode_1405.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
import java.util.PriorityQueue;
class Solution {
public String longestDiverseString(int a, int b, int c) {
// Priority queue to store the characters and their counts.
PriorityQueue<int[]> pq = new PriorityQueue<>((x, y) -> y[0] - x[0]);
if (a > 0) pq.offer(new int[]{a, 'a'});
if (b > 0) pq.offer(new int[]{b, 'b'});
if (c > 0) pq.offer(new int[]{c, 'c'});
StringBuilder result = new StringBuilder();
while (!pq.isEmpty()) {
int[] first = pq.poll();
// Check if last two characters are the same.
if (result.length() >= 2 && result.charAt(result.length() - 1) == first[1] &&
result.charAt(result.length() - 2) == first[1]) {
if (pq.isEmpty()) break; // No more valid characters.
// Pick the second character.
int[] second = pq.poll();
result.append((char) second[1]);
second[0]--;
if (second[0] > 0) pq.offer(second);
pq.offer(first);
} else {
result.append((char) first[1]);
first[0]--;
if (first[0] > 0) pq.offer(first);
}
}
return result.toString();
}
}