Simple and Elegant C++ Solution with Explanation
Consider the Below Example:\n\n\nWe now know that we gotta start with the character with the higher frequency.\n\n\nWhat did we understand from the last example
**Consider the Below Example:**\n![example1](https://assets.leetcode.com/users/images/1b59cb4e-1409-4afd-a61a-9997e7c0ffc1_1657303794.5330834.jpeg)\n\n**We now know that we gotta start with the character with the higher frequency.**\n\n![image](https://assets.leetcode.com/users/images/985148c0-46b1-42bb-a481-3b11ece2c570_1657303831.6448731.jpeg)\n**What did we understand from the last example?**\nWe not only have to consider the character with higher frequency first, but also have to make sure that we keep sandwiching the char with lower frequency once to maximize the appearance of the character with higher frequency.\n\n**The Code:**\n\n```\nclass Solution {\npublic:\n    string strWithout3a3b(int a, int b) {\n        int n = a + b;\n        string ans = "";\n        //I\'m making a priority queue just because I don\'t wanna worry about all the if and else.\n        priority_queue<pair<int, char>>q;\n\n        q.push({a, \'a\'});\n        q.push({b, \'b\'});\n\n        auto cur = q.top(); //now the cur will contain the char with highest freq.\n        q.pop();\n        auto prev = q.top();\n        q.pop();\n\n        while (ans.size() < n) {\n            //The below condition decides whether or not to add the cur element twice,\n            //i.e. if the cur element\'s frequency is greater than the prev, we will append it one more time.\n            if (cur.first > 0 && cur.first > prev.first) {\n                ans += cur.second;\n                --cur.first;\n            }\n            if (cur.first > 0) { //otherwise we\'ll add it just one time, i.e. sandwich it between\n                ans += cur.second;\n                --cur.first;\n            }\n            swap(cur, prev);\n\n        }\n        return ans;\n    }\n# };\n```\n\n**Follow-up problems:**\n[Leetcode 1405: Longest Happy String](http://leetcode.com/problems/longest-happy-string/)\n[Leetcode 767: Reorganize String](http://leetcode.com/problems/reorganize-string/)
['C', 'C++']