✅ [Python/C++/Java/Rust] count two types of words (with detailed comments)
\u2705 IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE.\n*\nThis solution employs a HashMap to count two types of reflections (mirror words). Time complexity is linear
**\u2705 IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE.**\n****\nThis solution employs a HashMap to count two types of reflections (mirror words). Time complexity is linear: **O(N)**. Space complexity is linear: **O(N)**.\n\n**Comment**. The idea of the solution consists in two main points:\n1. For every **asymmetric** word, there should be enough mirrored words. Thus, the number of such pairs is the minimum of counts for the word and its mirror. This allows us to construct *even palindromes*, eg., `dc ab ba cd`.\n2. For every **symmetric** word, we should check the evenness of its count. Odd count means that one instance of a symmetric word can be placed in the middle of an *odd palindrome*, e.g., `dc ab ff ba cd`. Note, that **only one** such odd word can be used in the middle.\n\n**Python.**\n```\nclass Solution:\n    def longestPalindrome(self, words: List[str]) -> int:\n        freq = Counter(words)                 # obtain frequencies\n        pp, p = 0, 0\n        for w, f in freq.items():\n            if w[0] == w[1]: \n                p = max(p, f % 2)             # odd-count symmetric word detector\n                pp += f//2 * 2                # count paired symmetric words\n            else:\n                pp += min(f, freq[w[::-1]])   # count paired asymmetric words\n        return (pp + p) * 2\n```\n\n<iframe src="https://leetcode.com/playground/mrtpUuBs/shared" frameBorder="0" width="800" height="580"></iframe>
[]