C++ | Easy | Intuition Explained
Intuition\nWe are given a words array which contains only strings each of the two letters. So basically have two conditions to check: \n\n1) When both letters a
# Intuition\nWe are given a words array which contains only strings each of the two letters. So basically have two conditions to check: \n\n1) When both letters are different\n2) When both letters are same\n\nSo at any point when we found the mirror of the word in the array we increase the ans by +4. \n\n    Example: abcdxxxxdcba\n\nAlso if at any point we found a word with same letters like aaa, bbb then just increase the ans by +2.\n\n    Example: xxxaaaxxx\n\n# Complexity\n- Time complexity:\nO(N), where N is the length of the array\n\n- Space complexity:\nO(N), where N is the length of the array\n\n# Code\n```\nclass Solution {\npublic:\n    int longestPalindrome(vector<string>& words) {\n        int ans = 0;\n        unordered_map<string,int> mp;\n        for(auto word : words)\n        {\n            string rev = word;\n            reverse(rev.begin(),rev.end());\n            if(mp[rev] > 0)\n            {\n                ans+=4;\n                mp[rev]--;\n            }\n            else \n            {\n                mp[word]++;\n            }\n        }\n        for(auto x : mp)\n        {\n            if(x.first[0] == x.first[1] && x.second > 0)\n            {\n                ans+=2;\n                break;\n            }\n        }\n        return ans;\n    }\n};\n```\n```\nif(liked(\u2764)==true) Upvote(\u2B06);\nelse Comment(\uD83D\uDCDD);\nreturn\xA0Thank\xA0You\uD83D\uDE01\n```
['C++']