Tuesday, December 27, 2022
A function, given a string s
which consists of lowercase or uppercase letters, to return the length of the longest palindrome that can be built with those letters.
func longestPalindrome(s string) int {
countList, maxLength := make(map[rune]int), 0
for _, run := range s {
// delete from countList in case the letter already recorded
if _, exists := countList[run]; exists {
delete(countList, run)
maxLength++
// register letters in countList
} else {
countList[run] = 1
}
}
// add new possible letter if countList is not empty
if len(countList) != 0 {
return maxLength * 2 + 1
}
return maxLength * 2
}