-
Notifications
You must be signed in to change notification settings - Fork 3
/
767.cpp
32 lines (30 loc) · 1011 Bytes
/
767.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
class Solution {
public:
string reorganizeString(string s) {
int n = s.size();
vector<int> counts(26, 0);
for (const auto& c : s) {
counts[c - 'a']++;
}
priority_queue<pair<int, char>> pq;
for (int i = 0; i < 26; ++i) {
if (counts[i] > 0) {
if (counts[i] > ((n + 1) / 2)) return "";
pq.push(make_pair(counts[i], i + 'a'));
}
}
string res;
while (pq.size() >= 2) {
auto first = pq.top();
pq.pop();
auto second = pq.top();
pq.pop();
res.push_back(first.second);
res.push_back(second.second);
if (first.first - 1 > 0) pq.push(make_pair(first.first - 1, first.second));
if (second.first - 1 > 0) pq.push(make_pair(second.first - 1, second.second));
}
if (!pq.empty()) res.push_back(pq.top().second);
return res;
}
};