forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1405-longest-happy-string.java
44 lines (43 loc) · 1.42 KB
/
1405-longest-happy-string.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
44
class Solution {
public String longestDiverseString(int a, int b, int c) {
String res = "";
PriorityQueue<Pair<Integer, Character>> maxHeap = new PriorityQueue<>(
(i1, i2) -> i2.getKey() - i1.getKey());
if (a > 0) {
maxHeap.add(new Pair<>(a, 'a'));
}
if (b > 0) {
maxHeap.add(new Pair<>(b, 'b'));
}
if (c > 0) {
maxHeap.add(new Pair<>(c, 'c'));
}
while (!maxHeap.isEmpty()) {
Pair<Integer, Character> item = maxHeap.poll();
Integer count = item.getKey();
Character ch = item.getValue();
if (res.length() > 1
&& ch.equals(res.charAt(res.length() - 1))
&& ch.equals(res.charAt(res.length() - 2))) {
if (maxHeap.isEmpty()) {
break;
}
Pair<Integer, Character> item2 = maxHeap.poll();
Integer count2 = item2.getKey();
Character ch2 = item2.getValue();
res += ch2;
count2 -= 1;
if (count2 > 0) {
maxHeap.add(new Pair<>(count2, ch2));
}
} else {
res += ch;
count -= 1;
}
if (count > 0) {
maxHeap.add(new Pair<>(count, ch));
}
}
return res;
}
}