-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
reorganize-string.cpp
41 lines (39 loc) · 1.3 KB
/
reorganize-string.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
35
36
37
38
39
40
41
// Time: O(nloga) = O(n), a is the size of alphabet
// Space: O(a) = O(1)
class Solution {
public:
string reorganizeString(string S) {
unordered_map<char, int> counts;
for (const auto& c : S) {
++counts[c];
}
if (any_of(counts.cbegin(), counts.cend(),
[&](const pair<char, int>& kvp) {
return kvp.second > (S.length() + 1) / 2;
})) {
return "";
}
string result;
priority_queue<pair<int, char>> max_heap;
for (const auto& kvp : counts) {
max_heap.emplace(kvp.second, kvp.first);
}
while (max_heap.size() > 1) {
char c1, c2;
int count1, count2;
tie(count1, c1) = max_heap.top(); max_heap.pop();
tie(count2, c2) = max_heap.top(); max_heap.pop();
if (result.empty() || c1 != result.back()) {
result.push_back(c1);
result.push_back(c2);
if (count1 - 1 > 0) {
max_heap.emplace(count1 - 1, c1);
}
if (count2 - 1 > 0) {
max_heap.emplace(count2 - 1, c2);
}
}
}
return max_heap.empty() ? result : result + max_heap.top().second;
}
};