1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-19 17:56:55 +02:00
LeetCode/go/longest-palindrome.go
Matej Focko bdc7307c3e
go: add «409. Longest Palindrome»
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-06-04 10:34:37 +02:00

26 lines
396 B
Go

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
}