Google Online Assessment 2021 (OA) - Longest Palindrome - Company-specific OAs / Google OA

https://algo.monster/problems/google-oa-longest-palindrome

I like C++ for this kind of thing.

int solution(std::vectorstd::string arr) {
using namespace std;
unordered_map<string, int> counts;

int count = 0;

for (auto& forwards : arr) {
    string backwards = {forwards.rbegin(), forwards.rend()};
    if (counts[backwards] > 0) {
        counts[backwards]--;
        count += 4;
    }
    else {
        counts[forwards]++;
    }
}

for (char x = 'a'; x <= 'z'; ++x) {
    if (counts[string({x, x})]) {
        return count + 2;
    }
}

return count;

}