Easy understanding C++ code with comments
We want to form a lexicographically largest string using the characters in a given string.\nBut we have to make sure that a letter is not repeated more than the
We want to form a lexicographically largest string using the characters in a given string.\nBut we have to make sure that a letter is not repeated more than the given limit in a row (i.e consecutively).\n\nSo we use a ***priority_queue*** to pop the element which has the highest priority. *If it\'s count is less than the repeatLimit,* we directly add to the ans.\n\n*But if it\'s count is greater than the repeatLimit*, we just add the repeatLimit amount of the present character and we pop out the next priority character and add a single letter to the ans.\nThis makes that a letter is not repeated more than limit in a row and lexicographically largest is maintained. After adding to the ans, if we have extra characters left out, we just push back into priority_queue for the next use.\n\nHere *if we are not able to pop*, the next priority character, we just return the ans so as we cannot add more than repeatedLimit charecters in a row.\n```\nclass Solution {\npublic:\n    string repeatLimitedString(string s, int k) { // k is the repeatLimit\n        int n = s.length();\n        unordered_map<char,int> m;\n        for(int i=0;i<n;i++) m[s[i]]++;\n        priority_queue<pair<char,int>> pq;\n        for(auto i: m){\n            pq.push({i.first,i.second}); // pushing the characters with their frequencies.\n        }\n        \n        string ans = "";\n        while(!pq.empty()){\n            char c1 = pq.top().first;\n            int n1 = pq.top().second;\n            pq.pop();\n                \n            int len = min(k,n1); // Adding characters upto minimum of repeatLimit and present character count.\n            for(int i=0;i<len;i++){ // adding the highest priority element to the ans.\n                ans += c1;\n            }\n            \n            char c2;\n            int n2=0;\n            if(n1-len>0){ // If the cnt of present character is more than the limit.\n                if(!pq.empty()){ //Getting the next priority character.\n                    c2 = pq.top().first;\n                    n2 = pq.top().second;\n                    pq.pop();\n                }\n                else{\n                    return ans; // if there is no another letter to add, we just return ans.\n                }\n                ans += c2; // Adding next priority character to ans.\n                \n                // If the elements are left out, pushing them back into priority queue for next use.\n                pq.push({c1,n1-len});\n                if(n2-1>0) pq.push({c2,n2-1}); \n            }\n        }\n        return ans;\n    }\n};\n```\n**Upvote if it helps!**
['Greedy', 'C', 'Heap (Priority Queue)']