go: add «3223. Minimum Length of String After Operations»

URL:	https://leetcode.com/problems/minimum-length-of-string-after-operations/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2025-01-13 11:53:04 +01:00
parent 61dfb7cbd5
commit 0ca8344c5a
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,29 @@
package main
func minimumLength(s string) int {
freqs := make([]int, 26)
for _, c := range s {
freqs[c-'a']++
}
length := len(s)
for _, c := range freqs {
if c <= 2 {
// cannot remove anything
continue
}
// middle character must be kept
c--
// if there's odd count left, there will be one more character kept
if c%2 != 0 {
c--
}
// rest can be safely removed
length -= c
}
return length
}