O(N) | Complete easy explanation with examples | c++
\nwords = ["ab","ty","yt","lc","cl","ba"]\n\n"ab", "ba"  |\n"ty", "yt"  |==> "abtylcclytba"   : we can make in any order  \n"lc", "cl"  |\n       \nIf reverse s
```\nwords = ["ab","ty","yt","lc","cl","ba"]\n\n"ab", "ba"  |\n"ty", "yt"  |==> "abtylcclytba"   : we can make in any order  \n"lc", "cl"  |\n       \nIf reverse string is in map, then make pair with it.\n1 pair = ("xy","yx") --> 4 characters.\n\nwords2 = ["aa","bb","aa","cc","cc","bb","aa","bb", "aa", "cc"]\n\n"aa", "aa"  |\n"bb", "bb"  |==> "aabbccaaccbbaa"   : we can make in any order  \n"cc", "cc"  |           --\n"aa", "aa"  |\n    \nHere extra strings are -> ["bb", "cc"]\n\nLets talk about strings like -> "aa", "cc", "bb"...\nCASE 1:\nIf they are the only strings left without pair formation, then we can consider only \n1 such string, which will be placed in middle. so, count += 2\n\nCASE 2:\nwe will find how many pairs are there, like -> (\'xx\',\'xx\')\nif mp[\'xx\'] is even, then there will be 0 such duplicate pair, and count += (pairs)*4, as 1 pair has 4 characters\nif mp[\'xx\'] is odd, count += (pairs)*4. Here, 1 string will not form pair, thats added to duplicate count\n```\n\n```\n\nclass Solution {\npublic:\n    int longestPalindrome(vector<string>& words) {\n        unordered_map<string, int>mp;\n        for(string str: words)  mp[str]++;      // frequency of every string\n        \n        // doubles = count duplicate strings\n        // count = count pairs --> ("xy", "yx")\n        // newStr = reversed string\n        int count, doubles = 0, ans = 0;        \n        string newStr;\n        \n        for(string str : words){\n            \n             newStr = "";\n             count = 0;\n             newStr += str[1];\n             newStr += str[0];\n            \n            if(mp[newStr] > 0){\n                 // If string is like -> "aa", "cc", "bb"...\n                 if(str == newStr){\n                     \n                    if(mp[str] % 2 == 0){\n                        count += 4*(mp[str]/2);\n                    }\n                     else{\n                        count += 4*((mp[str]-1)/2);\n                        doubles ++;\n                    }\n                }\n                // If string is like -> "xy", "yx"...\n                else{\n                    count += min(mp[str],mp[newStr]);\n                    count *= 4;\n                }\n                mp.erase(str);\n                mp.erase(newStr);\n            }\n            ans += count;   \n        }\n         \n        // taking 1 palindromic("xx") string if it exists \n        if(doubles > 0) ans += 2;\n        \n        return ans;\n        \n    }\n};\n```
['String']