1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-09 15:59:06 +01:00

go: add «1208. Get Equal Substrings Within Budget»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-05-28 13:46:34 +02:00
parent 03dc69e6ed
commit 2e2c8a758e
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,29 @@
package get_equal_substrings_within_budget
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
}