-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHappyString.cpp
More file actions
56 lines (56 loc) · 1.45 KB
/
HappyString.cpp
File metadata and controls
56 lines (56 loc) · 1.45 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
class Solution {
public:
string longestDiverseString(int a, int b, int c) {
priority_queue<pair<int,char>>pq;
string temp="";
pair<int,char>first,second;
if(a)
pq.push(make_pair(a,'a'));
if(b)
pq.push(make_pair(b,'b'));
if(c)
pq.push(make_pair(c,'c'));
if(pq.empty())
return temp;
while(pq.size()>1)
{
first = pq.top();
pq.pop();
second=pq.top();
pq.pop();
if(first.first>=2)
{
temp=temp+string(2,first.second);
first.first -= 2;
}
else if(first.first==1)
{
temp=temp+first.second;
first.first -= 1;
}
if((second.first>=2) && (second.first>=first.first))
{
temp=temp+string(2,second.second);
second.first -= 2;
}
else
{
temp=temp+second.second;
second.first -= 1;
}
if(first.first>0)
pq.push(first);
if(second.first>0)
pq.push(second);
}
if(pq.empty())
return temp;
first=pq.top();
pq.pop();
if(first.first>=2)
temp=temp+string(2,first.second);
else
temp=temp+first.second;
return temp;
}
};