Skip to content

Commit a0398a4

Browse files
Create longest_happy_strings.cpp
1 parent cc3155d commit a0398a4

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

longest_happy_strings.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
string longestDiverseString(int a, int b, int c) {
4+
priority_queue<pair<int, char>> pq;
5+
if(a != 0) pq.push({ a, 'a' });
6+
if(b != 0) pq.push({ b, 'b' });
7+
if(c != 0) pq.push({ c, 'c' });
8+
char prev1 = '#';
9+
char prev2 = '#';
10+
string res;
11+
while(!pq.empty())
12+
{
13+
auto [cnt1, ch1] = pq.top(); pq.pop();
14+
if(ch1 == prev1 && ch1 == prev2)
15+
{
16+
if(pq.empty()) return res;
17+
auto [cnt2, ch2] = pq.top(); pq.pop();
18+
res += ch2;
19+
prev1 = prev2;
20+
prev2 = ch2;
21+
pq.push({ cnt1, ch1 });
22+
if(--cnt2 > 0) pq.push({ cnt2, ch2 });
23+
}
24+
else
25+
{
26+
prev1 = prev2;
27+
prev2 = ch1;
28+
res += ch1;
29+
if(--cnt1 > 0) pq.push({ cnt1, ch1 });
30+
}
31+
}
32+
return res;
33+
}
34+
};

0 commit comments

Comments
 (0)