3
string reorganizeString(string s) {4
// Step1: insert elements to the map so that we will get the frequency5
unordered_map<char, int> mp;10
// Step2: Create a max heap to store all the elements according to there12
priority_queue<pair<int, char>> pq;15
pq.push({it.second, it.first});18
// Step3: Now take two elements from the heap and do this till the map20
// why one : cause we are taking two top elements like pq.top is a then21
// will pop and again pq.top is b and will add to answer23
while (mp.size() > 1) {24
// get the top two elements from heap25
char ch1 = pq.top().second;28
char ch2 = pq.top().second;32
// now reduce the size in the mp33
// now we have added two char in the ans so reduce the cound in map37
// Now check if it's size is still greater than 0 then push38
// if size is greater in map than 0 then we again need to push into the39
// map so that we can make the ans string41
pq.push({mp[ch1], ch1});43
// if the size is 0 then decrese the map means erase the map47
pq.push({mp[ch2], ch2});53
// Step4 : Now check wheather any element is present into it54
// Now we have zero size of the map so check top element size is greater55
// than 1 or not if greater the we cannot split it since it''s only that56
// char if not add to ans58
if (mp[pq.top().second] > 1) {61
ans += pq.top().second;