Greedy approach | O(n) | Explained
Explanation\nLet\'s first understand what a palindrome is: a palindrome is a word that, if we read it from left to right, and from right to left, it will read t
# Explanation\nLet\'s first understand what a palindrome is: a palindrome is a word that, if we read it from left to right, and from right to left, it will read the same. Examples are "madam" and "level".\n\nThe problem says we should return the length of the palindrome that **can be built** from a given string `s`. We don\'t look for palindromes but build them.\n\nIf you look at the palindromes we\'ve written, an interesting property that pops up is that all the letters come up an `even` number of times, and only 1 letter will be `odd` if the string is odd, like "level". \n\n# Greedy approach\nWe can create a counter of the string. `key=occurrences`\nIf there are even occurrences for the given character, we simply add the number of occurrences  to the result, as we can take all the letters. If there are odd occurrences  for the given character, we add everything but 1 (take 4 out of 5). If we met atleast one odd occurrence, we should add +1 to our end result to maximize the possible palindrome that can be built.\n\n# Code\n```\nfrom collections import Counter\n\nclass Solution:\n    def longestPalindrome(self, s: str) -> int:\n        s_counter = Counter(s)\n        # We take all even, and biggest odd\n        odd = 0\n        result = 0\n        for v in s_counter.values():\n            if v % 2 == 0: result += v\n            else:\n                odd = 1\n                result += (v-1) # We take all events and 1 left over\n        \n        return result + odd\n```\n\n# Complexity analysis\nTime - `O(n)` as we iterate over the string 2 times. 1 to create the counter, and another to count the occurrences.\nSpace - `O(n)/O(1)` the counter space. Technically since we are bound to only upper and lower English letters than it will be O(52). But for generic n input, the space complexity will be O(n).
['Greedy', 'Python']