forked from ghostmkg/programming-language
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Longest_Happy_String.cpp
39 lines (38 loc) · 1.09 KB
/
Longest_Happy_String.cpp
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
class Solution {
public:
string longestDiverseString(int a, int b, int c) {
priority_queue<pair<int, char>> pq;
if (a > 0)
pq.push({a, 'a'});
if (b > 0)
pq.push({b, 'b'});
if (c > 0)
pq.push({c, 'c'});
string s;
while (!pq.empty()) {
int curmax = pq.top().first;
char curchar = pq.top().second;
pq.pop();
if (s.length() >= 2 && s[s.length() - 2] == curchar &&
s[s.length() - 1] == curchar) {
if (pq.empty())
break;
int nextmax = pq.top().first;
char nextchar = pq.top().second;
pq.pop();
s.push_back(nextchar);
nextmax--;
if (nextmax > 0) {
pq.push({nextmax, nextchar});
}
} else {
s.push_back(curchar);
curmax--;
}
if (curmax > 0) {
pq.push({curmax, curchar});
}
}
return s;
}
};