Swift💯
Heap of Character Frequencies (accepted answer)
**Heap of Character Frequencies (accepted answer)**\n```\nimport Collections\nstruct HeapItem: Comparable {\n    var ch: Character, count:Int\n    init(_ a:Character, _ b:Int) {ch=a;count=b}\n    static func <(l:Self, r:Self) -> Bool { l.ch < r.ch }\n}\nclass Solution {\n    func repeatLimitedString(_ s: String, _ repeatLimit: Int) -> String {\n        let freq = Dictionary(zip(s,repeatElement(1,count:s.count)), uniquingKeysWith: +)\n        var heap = Heap(freq.map(HeapItem.init))\n        var result = ""\n        \n        while var item = heap.popMax() {\n            while true {\n                result += String(repeating: item.ch, count: min(item.count, repeatLimit))\n                item.count -= repeatLimit\n                guard item.count > 0, let next = heap.popMax() else { break }\n                result.append(next.ch) // poke one char from next largest character\n                if next.count > 1 {\n                    heap.insert(HeapItem(next.ch, next.count-1))\n                }\n            }\n        }\n        return result\n    }\n}\n```
['Swift']