arcbjorn

thoughtbook

RSS Feed

Tuesday, December 27, 2022

Longest palindrome

Leetcode 409 problem.

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.

Go

  • Time complexity: O(n)O(n) - n is a length of a string
  • Auxiliary space: O(n)O(n) - n is a length of a string
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
}
Creative Commons Licence