go: add «409. Longest Palindrome»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
78f4a78500
commit
bdc7307c3e
1 changed files with 26 additions and 0 deletions
26
go/longest-palindrome.go
Normal file
26
go/longest-palindrome.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
package longest_palindrome
|
||||
|
||||
func longestPalindrome(s string) int {
|
||||
getFreqs := func() map[rune]int {
|
||||
freqs := make(map[rune]int)
|
||||
for _, c := range s {
|
||||
freqs[c]++
|
||||
}
|
||||
return freqs
|
||||
}
|
||||
freqs := getFreqs()
|
||||
|
||||
length := 0
|
||||
|
||||
usedOdd := false
|
||||
for _, count := range freqs {
|
||||
length += 2 * (count / 2)
|
||||
|
||||
if !usedOdd && count%2 == 1 {
|
||||
length += 1
|
||||
usedOdd = true
|
||||
}
|
||||
}
|
||||
|
||||
return length
|
||||
}
|
Loading…
Reference in a new issue