At first , dp comes out. Then, Sliding windows maybe a better idea to solve this.
I use a to store the information of differences, howerver, that is not necessary. We could simply this by only one glance, but, I found the running time is about the same.
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
a = [abs(ord(s[i]) - ord(t[i])) for i in range(len(s))]
ans, tmp, j = 0, 0 , 0
for i in range(len(a)):
tmp += a[i]
while tmp > maxCost:
tmp -= a[j]
j += 1
ans = max(ans , i-j+1)
return ans