JAVASCRIPT✅EASY EXPLANATION✅[58ms]✅94.22%BEATS
\n\n# Explanation\n1. The function takes a string s as input and initializes a variable answer to 0, which will be used to store the length of the longest palin
![Screenshot 2023-03-31 at 02.13.52.png](https://assets.leetcode.com/users/images/243c5f57-56eb-4825-8c38-b56b0979cdd0_1680222075.3453658.png)\n\n# Explanation\n1. The function takes a string s as input and initializes a variable answer to 0, which will be used to store the length of the longest palindrome.\n\n2. An empty object hashTable is initialized, which will be used to store the frequency of each character in the input string.\n\n3. The function loops through each character char in the input string s.\n\n4. If the character char already exists in the hashTable, then its value is incremented by 1. If not, it is initialized to 0 and then incremented by 1.\n\n5. If the frequency of char is even, the answer variable is incremented by 2. This is because a palindrome can be formed by pairing two characters of the same type.\n\n6. After looping through all characters in the input string, the function checks if the length of the input string is greater than answer. If it is, then a single character can be added to the palindrome, so answer is incremented by 1.\n\n7. The final value of answer is returned as the length of the longest palindrome that can be formed using the characters in the input string.\n\n# Complexity\n- Time complexity:\nIt is O(n), where n is the length of the input string s. This is because the function loops through each character in the input string exactly once, and all other operations (object property access, comparison, addition, etc.) are constant time operations.\n\n- Space complexity:\nIt is also O(n), because the hashTable object will contain at most n entries (one for each character in the input string), and the size of each entry is constant. Therefore, the space used by the function will grow linearly with the size of the input string.\n\n# Code\n```\n/**\n * @param {string} s\n * @return {number}\n */\nvar longestPalindrome = function (s) {\n  let answer = 0;\n  let hashTable = {};\n  for (let char of s) {\n    hashTable[char] = (hashTable[char] || 0) + 1;\n    if (hashTable[char] % 2 === 0) {\n      answer += 2;\n    }\n  }\n  return s.length > answer ? answer + 1 : answer;\n};\n\n```
['JavaScript']