2024-06-16 11:44:08 +02:00
|
|
|
package main
|
2024-05-28 13:46:34 +02:00
|
|
|
|
|
|
|
func equalSubstring(s string, t string, maxCost int) int {
|
|
|
|
abs := func(x int) int {
|
|
|
|
return max(x, -x)
|
|
|
|
}
|
|
|
|
|
|
|
|
getCost := func(i int) int {
|
|
|
|
return abs(int(s[i]) - int(t[i]))
|
|
|
|
}
|
|
|
|
|
|
|
|
mostOptimal := 0
|
|
|
|
|
|
|
|
begin := 0
|
|
|
|
|
|
|
|
cost := 0
|
|
|
|
for i, _ := range t {
|
|
|
|
cost += getCost(i)
|
|
|
|
|
|
|
|
for cost > maxCost {
|
|
|
|
cost -= getCost(begin)
|
|
|
|
begin++
|
|
|
|
}
|
|
|
|
|
|
|
|
mostOptimal = max(mostOptimal, i-begin+1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return mostOptimal
|
|
|
|
}
|