Swift | Greedy
Code
# Code\n```swift []\nclass Solution {\n    func repeatLimitedString(_ s: String, _ repeatLimit: Int) -> String {\n        var repeatCount = 0\n        var overflow = [Character]()\n        var res = ""\n\n        // Greedily, go through each character by descending order.\n        for c in s.sorted(by: >) {\n            if repeatCount == repeatLimit && res.last == c {\n                // Unable to use "c" for now. Save it for later.\n                overflow.append(c)\n                continue\n            }\n\n            append(c)\n            for _ in 0..<min(repeatLimit, overflow.count) {\n                append(overflow.removeLast())\n            }\n        }\n\n        return res\n\n        func append(_ c: Character) {\n            if res.last != c { repeatCount = 0 }\n            res.append(c)\n            repeatCount += 1\n        }\n    }\n}\n```
['Swift']