✅C++ || Using Map 🗝|| ⌛Fast & Easy ||
Intuition :\nThings to be kept in mind : \n1.Strings like "ab" can be included if and only if string "ba" is present.so we must add \n#### 2 \xD7 2 \xD7 (min(st
# Intuition :\nThings to be kept in mind : \n1.Strings like "ab" can be included if and only if string "ba" is present.so we must add \n#### 2 \xD7 2 \xD7 (min(string_count , reverse_string_count)) \nto answer for such strings .\n\n2.Strings like "aa" can be included even number of times , we can add\n#### 2 \xD7 2 \xD7 (string_count/2)\nto the answer. There can only be atmost one such string which can be included odd number of times and if any of such a string of type "aa" is present odd number of times add an extra 2 to the ans. \n```\ne.g we have : "ab","cc","cc","cc","mm","mm","ba" \nso we can make longest palindrome as :\n"abmmccccccmmba" .\n```\n\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int longestPalindrome(vector<string>& words) {\n        map<string , int> mp;\n        for(string s: words){\n            mp[s]++;\n        }\n\n        int maxi1=0;\n        int maxi2=0;\n        bool chk=false;\n        for(auto m : mp){\n            if((m.first)[0]==(m.first)[1]){\n                maxi2+=(2*2*(m.second/2));\n                if(mp[m.first]&1){\n                    chk=true;\n                }\n                cout<<m.first<<" m2:"<<maxi2<<endl;\n            }\n            else{\n                string temp="";\n                temp+=(m.first)[1];\n                temp+=(m.first)[0];\n                maxi1+=2*2*(min(mp[m.first],mp[temp]));\n                mp[temp]=0;\n                cout<<m.first<<" m1:"<<maxi1<<endl;\n            }\n        }\n        if(chk){\n            return maxi1+maxi2+2;\n        }\n        return maxi1+maxi2;\n    }\n};\n```\n\n\n### DO UPOTE if it helped you .\n![image.png](https://assets.leetcode.com/users/images/09009d33-1631-43b5-8ac7-0f6804212437_1667467277.9700933.png)\n\n\n
['Ordered Map', 'C++']