# LeetCode Notes: Longest Palindrome

## Question

Given a string `s` which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters.

Letters are case sensitive, for example, `"Aa"` is not considered a palindrome here.

Example 1:

Input: s = "abccccdd"

Output: 7

Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.

Example 2:

Input: s = "a"

Output: 1

Example 3:

Input: s = "bb"

Output: 2

Constraints:

• `1 <= s.length <= 2000`
• `s` consists of lowercase and/or uppercase English letters only.

## Solution

Analysis:

1. First count the number of occurrences of each letter
2. The even number of letters can be placed directly on both sides of the palindrome string, so you can use it directly. The odd number of letters minus one becomes an even number, which can also be used
3. In the final special treatment, if there are an odd number of letters, the largest odd number of letters can be placed in the middle position, that is, the final result needs to be +1

Code:

``````/**
* @param {string} s
* @return {number}
*/
var longestPalindrome = function(s) {
// Used to count the number of times each letter appears
const count = {}
for(let i = 0; i <s.length; i++){
const str = s[i]
// Repeated letters, number of times +1
if(str in count){
count[str] += 1
}else{// New letters start counting
count[str] = 1
}
}
let odd = false // Whether to include odd numbers
let result = 0 // Result statistics
for(let letter in count){
const num = count[letter]
// Letters with an odd number of occurrences, minus one is an even number, and can be placed on both sides of the palindrome
if(num % 2 === 1){
result += num-1
odd = true
}else if(num % 2 === 0){// Take an even number of letters
result += num
}

}
// As long as there is an odd number of letters, the largest odd number of letters can be placed in the middle position, so there will be plus one
return odd ? result + 1: result
};
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.75558573,"math_prob":0.9898687,"size":1958,"snap":"2023-40-2023-50","text_gpt3_token_len":499,"char_repetition_ratio":0.16683726,"word_repetition_ratio":0.10614525,"special_character_ratio":0.27681306,"punctuation_ratio":0.10962567,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99340254,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-10T06:47:20Z\",\"WARC-Record-ID\":\"<urn:uuid:2e4dfbf6-e471-400c-a369-9054767dce8f>\",\"Content-Length\":\"46198\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b72883e1-7321-4b29-a540-e304f6618384>\",\"WARC-Concurrent-To\":\"<urn:uuid:05f60b67-97d9-411b-8471-edb68d58c64e>\",\"WARC-IP-Address\":\"139.196.97.2\",\"WARC-Target-URI\":\"https://lwebapp.com/en/post/leetcode-longest-palindrome\",\"WARC-Payload-Digest\":\"sha1:BVZTQL5B4KO5M3VT7ULRN7WUU3TODHXZ\",\"WARC-Block-Digest\":\"sha1:Z5C6TLOYTAA3DLXTDONHGISUIIPIMMOH\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679101282.74_warc_CC-MAIN-20231210060949-20231210090949-00324.warc.gz\"}"}