Kotlin. Beats 100% (150 ms). Sliding window + static "diffs" array
\n\n# Code\n\nclass Solution {\n    fun equalSubstring(s: String, t: String, maxCost: Int): Int {\n        for (i in 0 until s.length) {\n            diffs[i] =
![image.png](https://assets.leetcode.com/users/images/83da60f3-7df8-4c2a-894a-75156861e61a_1716930241.0328887.png)\n\n# Code\n```\nclass Solution {\n    fun equalSubstring(s: String, t: String, maxCost: Int): Int {\n        for (i in 0 until s.length) {\n            diffs[i] = Math.abs(s[i] - t[i])\n        }\n        var maxLen = -1\n        var diff = 0\n        var left = diff\n        var right = left\n        while (right < s.length) {\n            diff += diffs[right]\n            while (diff > maxCost) {\n                diff -= diffs[left]\n                left++\n            }\n            maxLen = Math.max(maxLen, -left + right)\n            right++\n        }\n        return maxLen + 1\n    }\n\n    companion object {\n        val diffs = IntArray(100_001)\n    }\n}\n```
['Kotlin']