We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent cc3155d commit a0398a4Copy full SHA for a0398a4
longest_happy_strings.cpp
@@ -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
27
+ prev2 = ch1;
28
+ res += ch1;
29
+ if(--cnt1 > 0) pq.push({ cnt1, ch1 });
30
31
32
+ return res;
33
34
+};
0 commit comments