[Accepted] Swift
\nclass Solution {\n    func numberOfSubstrings(_ s: String, _ k: Int) -> Int {\n        \n        let s = Array(s)\n        var ch = [s[0]: 1]\n        var r =
```\nclass Solution {\n    func numberOfSubstrings(_ s: String, _ k: Int) -> Int {\n        \n        let s = Array(s)\n        var ch = [s[0]: 1]\n        var r = 0\n        var res = 0\n\n        for l in 0...s.count - k {\n            if l > 0 {\n                ch[s[l - 1]] = (ch[s[l - 1]]! > 0) ? (ch[s[l - 1]]! - 1) : nil\n            }\n            if ch.values.max()! < k {\n                while r < s.count {\n                    r += 1\n                    guard r < s.count else { break }\n                    ch[s[r], default: 0] += 1\n                    guard ch[s[r]]! != k else { break }\n                }\n            }\n            guard r < s.count else { break }\n            res += s.count - r\n        }\n\n        return res\n    }\n}\n```
['Swift']