代码拉取完成,页面将自动刷新
// 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;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。