diff --git a/go/longest-palindrome.go b/go/longest-palindrome.go new file mode 100644 index 0000000..bc13910 --- /dev/null +++ b/go/longest-palindrome.go @@ -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 +}