Posted on

Description

Submission

class Solution {
public:
    int longestPalindrome(vector<string>& words) {
        unordered_map<string, int> Map;

        int ret = 0;
        for(auto& word: words) {
            string reversed = word.substr(1, 1) + word.substr(0, 1);
            if(Map[reversed] > 0) {
                --Map[reversed];
                ret += 4;
            } else {
                ++Map[word];
            }
        }

        for(auto& [word, freq]: Map) {
            if(word[0] == word[1] && freq > 0) {
                ret += 2;
                break;
            }
        }

        return ret;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *