From c2b4298de87131b1bb4db24e4d1f36dff37d4c47 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Fri, 12 Jul 2024 11:09:11 +0200 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB1717.=20Maximum=20Score=20Fro?= =?UTF-8?q?m=20Removing=20Substrings=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- go/maximum-score-from-removing-substrings.go | 39 ++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 go/maximum-score-from-removing-substrings.go diff --git a/go/maximum-score-from-removing-substrings.go b/go/maximum-score-from-removing-substrings.go new file mode 100644 index 0000000..ecb46f5 --- /dev/null +++ b/go/maximum-score-from-removing-substrings.go @@ -0,0 +1,39 @@ +package main + +func maximumGain(s string, x int, y int) int { + reverseString := func(s string) string { + runes := []rune(s) + for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { + runes[i], runes[j] = runes[j], runes[i] + } + return string(runes) + } + + // swap for bigger score + if x < y { + x, y = y, x + s = reverseString(s) + } + + aCount, bCount, points := 0, 0, 0 + for _, c := range s { + if c == 'a' { + aCount++ + } else if c == 'b' { + if aCount > 0 { + aCount-- + points += x + } else { + bCount++ + } + } else { + points += y * min(bCount, aCount) + aCount = 0 + bCount = 0 + } + } + + points += y * min(bCount, aCount) + + return points +}