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:
parent
61dfb7cbd5
commit
0ca8344c5a
1 changed files with 29 additions and 0 deletions
29
go/minimum-length-of-string-after-operations.go
Normal file
29
go/minimum-length-of-string-after-operations.go
Normal 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue