1
var repeatLimitedString = function (s, repeatLimit) {4
for (const char of s) {5
const ascii = char.charCodeAt(0) - 97;7
if (!map.has(ascii)) map.set(ascii, 0);8
map.set(ascii, map.get(ascii) + 1);11
const maxHeap = new MaxPriorityQueue({ priority: (x) => x[0] });13
map.forEach((count, ascii) => maxHeap.enqueue([ascii, count]));15
let resLargestStr = "";17
while (!maxHeap.isEmpty()) {18
let [topASCII, topCount] = maxHeap.dequeue().element;20
const topChar = String.fromCharCode(topASCII + 97);22
if (topCount > repeatLimit) {23
if (maxHeap.isEmpty()) {24
resLargestStr += topChar.repeat(repeatLimit);29
let [secondASCII, secondCount] = maxHeap.dequeue().element;30
const secondChar = String.fromCharCode(secondASCII + 97);32
resLargestStr += topChar.repeat(repeatLimit);33
resLargestStr += secondChar;35
topCount -= repeatLimit;38
maxHeap.enqueue([topASCII, topCount]);39
if (secondCount > 0) maxHeap.enqueue([secondASCII, secondCount]);41
resLargestStr += topChar.repeat(topCount);