Longest Palindrome by Concatenating Two Letter Words LeetCode solution in C++
1class Solution {2public:3int longestPalindrome(vector<string> &words) {4int count[26][26] = {};5int ans = 0;67for (auto w : words) {8int a = w[0] - 'a';9int b = w[1] - 'a';1011if (count[b][a]) {12ans += 4;13count[b][a]--; // decrement the count as we found mirror word14} else15count[a][b]++; // increment the current word count if we not find any16// mirror word17}1819for (int i = 0; i < 26; i++) {20if (count[i][i]) {21ans += 2;22break;23}24}2526return ans;27}28};