mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 08:27:30 +08:00
31 lines
540 B
Go
31 lines
540 B
Go
package leetcode
|
|
|
|
func equalSubstring(s string, t string, maxCost int) int {
|
|
left, right, res := 0, -1, 0
|
|
for left < len(s) {
|
|
if right+1 < len(s) && maxCost-abs(int(s[right+1]-'a')-int(t[right+1]-'a')) >= 0 {
|
|
right++
|
|
maxCost -= abs(int(s[right]-'a') - int(t[right]-'a'))
|
|
} else {
|
|
res = max(res, right-left+1)
|
|
maxCost += abs(int(s[left]-'a') - int(t[left]-'a'))
|
|
left++
|
|
}
|
|
}
|
|
return res
|
|
}
|
|
|
|
func max(a int, b int) int {
|
|
if a > b {
|
|
return a
|
|
}
|
|
return b
|
|
}
|
|
|
|
func abs(a int) int {
|
|
if a > 0 {
|
|
return a
|
|
}
|
|
return -a
|
|
}
|