-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDay-231.cpp
35 lines (34 loc) · 1.04 KB
/
Day-231.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
using pci = pair<char,int>;
bool compare(pci&a , pci&b){
return a.second < b.second;
}
class Solution {
public:
string reorganizeString(string s) {
map<char,int>m;
for(auto&itr:s) {
m[itr]++;
}
priority_queue<pci,vector<pci> , function<bool(pci&,pci&)>> pq(compare);
for(auto&itr:m) {
pq.push(itr);
}
string res;
while (pq.size() > 1) {
pci charOne = pq.top(); pq.pop();
pci charTwo = pq.top(); pq.pop();
res+=charOne.first; charOne.second--;
res+=charTwo.first; charTwo.second--;
if (charOne.second) pq.push(charOne);
if (charTwo.second) pq.push(charTwo);
}
if (pq.size()) {
pci ch = pq.top(); pq.pop();
if(ch.second >=2) {
return "";
}
res+=ch.first;
}
return res;
}
};