JS fast frequency mapping solution
Runtime269 ms\nBeats\n84.44%\nMemory56.3 MB\nBeats\n85.88%\n\n### Intuition\n Describe your first thoughts on how to solve this problem. \n- All the letters are
Runtime269 ms\nBeats\n84.44%\nMemory56.3 MB\nBeats\n85.88%\n\n### Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- All the letters are grouped into pairs, so each pair included on one side has a *reflection* (letters switched) pair on the other side.\n- The exception to this is that a *matched pair* (aa,bb,etc) may occupy the center if there is an odd number of pairings.\n- No non-matched pairing may occur more times than it\'s reflection.\n- As matched pairings reflect themselves, all matched pairings (except 1) must occur an even number of times.\n\n\n### Approach\n<!-- Describe your approach to solving the problem. -->\n- Keep a count of the *pw count*, the number of pairings actually used in the palindrome, since we don\'t need to build the string.\n- Frequency map the matched and non-matched pairs separately.\n- Add the max of each non-matched pairing\'s count and it\'s reflection\'s count (x2) to the pw count.\n- Add each matched pairing\'s count (rounded down to the nearest even number) to the pw count, then add 1 if there were any odd counts.\n- Return 2 x the pw count.\n\n\n### Complexity\nO(n) O(n)\n\n### Code\n```\n/**\n * @param {string[]} words\n * @return {number}\n */\nvar longestPalindrome = function(words) \n{\n    /* Count the matched and non-matched letter pairs separately. */\n    let m_pairs = new Map(), nm_pairs = new Map();\n    for(let word of words)\n    {\n        if(word[0] === word[1])\n        {\n            let cnt = m_pairs.get(word) || 0;\n            m_pairs.set(word, cnt+1);\n        }\n        else\n        {\n            let cnt = nm_pairs.get(word) || 0;\n            nm_pairs.set(word, cnt+1);\n        }\n    }\n\n    /* Count the total # of non-matched pairs with reflections. */\n    let pw = 0, deleted = new Set();\n    for (const [word, wcnt] of nm_pairs)\n    {\n        if(deleted.has(word))\n            continue;\n        let refl = word[1] + word[0];\n        let reflcnt = nm_pairs.get(refl) || 0;\n        pw += 2*Math.min(wcnt, reflcnt);\n        deleted.add(refl);\n    }\n\n    /* Count the total matched pairs - if any have an odd count, one of them can go in the middle. */\n    let hasOdd = false;\n    for(let [word, wcnt] of m_pairs)\n    {\n        if(wcnt%2 === 1)\n        {\n            hasOdd = true;\n            wcnt--;\n        }\n        pw += wcnt;\n    }\n    if(hasOdd)\n        pw++;\n\n    return pw*2;\n};\n```
['JavaScript']