From 2e2c8a758ee617ffc9a6d00740f7611fcf69b065 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Tue, 28 May 2024 13:46:34 +0200 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB1208.=20Get=20Equal=20Substri?= =?UTF-8?q?ngs=20Within=20Budget=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- go/get-equal-substrings-within-budget.go | 29 ++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 go/get-equal-substrings-within-budget.go diff --git a/go/get-equal-substrings-within-budget.go b/go/get-equal-substrings-within-budget.go new file mode 100644 index 0000000..369627e --- /dev/null +++ b/go/get-equal-substrings-within-budget.go @@ -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 +}