1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-19 01:36:57 +02:00

go: add «1717. Maximum Score From Removing Substrings»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-07-12 11:09:11 +02:00
parent f6b4c01bbd
commit c2b4298de8
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -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
}