You are given an array of strings words. Each element of words consists of two lowercase English letters.

Create the longest possible palindrome by selecting some elements from words and concatenating them in any order. Each element can be selected at most once.

Return the length of the longest palindrome that you can create. If it is impossible to create any palindrome, return 0.

A palindrome is a string that reads the same forward and backward.

&nbsp;
Example 1:

Input: words = ["lc","cl","gg"]
Output: 6
Explanation: One longest palindrome is "lc" + "gg" + "cl" = "lcggcl", of length 6.
Note that "clgglc" is another longest palindrome that can be created.


Example 2:

Input: words = ["ab","ty","yt","lc","cl","ab"]
Output: 8
Explanation: One longest palindrome is "ty" + "lc" + "cl" + "yt" = "tylcclyt", of length 8.
Note that "lcyttycl" is another longest palindrome that can be created.


Example 3:

Input: words = ["cc","ll","xx"]
Output: 2
Explanation: One longest palindrome is "cc", of length 2.
Note that "ll" is another longest palindrome that can be created, and so is "xx".


&nbsp;
Constraints:


	1 &lt;= words.length &lt;= 105
	words[i].length == 2
	words[i] consists of lowercase English letters.


class Solution(object):
    def longestPalindrome(self, words):
        wc = collections.Counter(words)
        aa = 0  # count how many words contain only two identical letters like 'aa'
        center = 0  # if one count of 'aa' is odd, that means it can be the center of the palindrome, answer can plus 2
        abba = 0 # count how many word pairs like ('ab', 'ba') and they can put on both sides respectively

        for w, c in wc.items():
            if w[0] == w[1]: # like 'aa', 'bb', ...
                aa += c // 2 * 2 # if there are 3 'aa', we can only use 2 'aa' put on both sides respectively
                # if one count of 'aa' is odd, that means it can be the center of the palindrome, answer can plus 2
                if c % 2 == 1: center = 2
            else:
                abba += min(wc[w], wc[w[::-1]]) * 0.5  # will definitely double counting
        return aa * 2 + int(abba) * 4 + center
class Solution {
  public int longestPalindrome(String[] words) {
    int[][] freq = new int[26][26];//array for all alphabet combinations
    for (String word : words)
      freq[word.charAt(0) - 'a'][word.charAt(1) - 'a']++;// here we first increase the freq for every word
    int left = 0;//to store freq counts
    boolean odd = false;
    for (int i = 0; i != 26; i++) {//iterate over our array
      odd |= (freq[i][i] & 1) == 1;//means odd number of freq for similar words are there
      left += freq[i][i] / 2;
      for (int j = i + 1; j != 26; j++)//nested iteration to find non similar pairs
        left += Math.min(freq[i][j], freq[j][i]);//taking min times from both present
    }
    int res = left * 2 * 2;//res from total freq found!!
    if (odd){
        res+=2;// if odd then adding 2
    }
    return res;
  }
}
class Solution {
public:
    int longestPalindrome(vector<string>& words) {

       int count[26][26] = {};
       int ans =0;

            for(auto w : words){
                int a = w[0] - 'a';
                int b = w[1] - 'a';

                if(count[b][a]){
                    ans+= 4;
                    count[b][a]--; // decrement the count as we found mirror word
                }else
                    count[a][b]++; //increment the current word count if we not find any mirror word
            }

            for(int i=0;i<26;i++){
                if(count[i][i]){
                    ans+=2;
                    break;
                }
             }

    return ans;

    }
};
var longestPalindrome = function(words) {
    const n = words.length;
    const map = new Map();

    let len = 0;

    for (const word of words) {
         const backward = word.split("").reverse().join("");

        if (map.has(backward)) {
            len += (word.length * 2);
            map.set(backward, map.get(backward) - 1);

            if (map.get(backward) === 0) map.delete(backward);
        }
        else {
            if (!map.has(word)) map.set(word, 0);
            map.set(word, map.get(word) + 1);
        }
    }

    let maxLenSelfPalindrome = 0;

    for (const word of map.keys()) {
        if (isPalindrome(word)) {
            maxLenSelfPalindrome = Math.max(maxLenSelfPalindrome, word.length);
        }
    }

    return len + maxLenSelfPalindrome;

    function isPalindrome(word) {
        let left = 0;
        let right = word.length - 1;

        while (left < right) {
            if (word[left] != word[right]) return false;
            left++;
            --right;
        }

        return true;
    }
};
Longest Palindrome by Concatenating Two Letter Words