<p>You are given an array of strings <code>words</code>. Each element of <code>words</code> consists of <strong>two</strong> lowercase English letters.</p>

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

<p>Return <em>the <strong>length</strong> of the longest palindrome that you can create</em>. If it is impossible to create any palindrome, return <code>0</code>.</p>

<p>A <strong>palindrome</strong> is a string that reads the same forward and backward.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> words = [&quot;lc&quot;,&quot;cl&quot;,&quot;gg&quot;]
<strong>Output:</strong> 6
<strong>Explanation:</strong> One longest palindrome is &quot;lc&quot; + &quot;gg&quot; + &quot;cl&quot; = &quot;lcggcl&quot;, of length 6.
Note that &quot;clgglc&quot; is another longest palindrome that can be created.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> words = [&quot;ab&quot;,&quot;ty&quot;,&quot;yt&quot;,&quot;lc&quot;,&quot;cl&quot;,&quot;ab&quot;]
<strong>Output:</strong> 8
<strong>Explanation:</strong> One longest palindrome is &quot;ty&quot; + &quot;lc&quot; + &quot;cl&quot; + &quot;yt&quot; = &quot;tylcclyt&quot;, of length 8.
Note that &quot;lcyttycl&quot; is another longest palindrome that can be created.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> words = [&quot;cc&quot;,&quot;ll&quot;,&quot;xx&quot;]
<strong>Output:</strong> 2
<strong>Explanation:</strong> One longest palindrome is &quot;cc&quot;, of length 2.
Note that &quot;ll&quot; is another longest palindrome that can be created, and so is &quot;xx&quot;.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= words.length &lt;= 10<sup>5</sup></code></li>
	<li><code>words[i].length == 2</code></li>
	<li><code>words[i]</code> consists of lowercase English letters.</li>
</ul>

// class Solution {
// public:
//     int longestPalindrome(vector<string>& words) {
//         unordered_map<string,int> mpp;
//         for(auto it : words){
//             mpp[it]++;
//         }
//         bool centerUsed = false;
//         int ans = 0;
//         for(auto it : words){
//             string rev = it;
//             reverse(begin(it), end(it));
//          if(rev != it){   // ab , ba - 4 length hogi 
//             if(mpp[it] > 0 && mpp[rev] > 0){
//                 ans += 4;
//                 mpp[rev]--;
//                 mpp[it]--;
//             }
//          }
//           else{
//             if(mpp[it] >= 2){  // abcc ccba  
//                mpp[it]-= 2;
//                ans +=4;
//             }
//             else if(mpp[it] ==  1 && centerUsed == false){
//                 mpp[it]--;
//                 ans += 2;
//                 centerUsed=true;
//             }
//            }

//         }
//         return ans;
//     }
// };



class Solution {
public:
    int longestPalindrome(vector<string>& words) {
        // Create a frequency array of size 676 to represent all 2-letter words
        vector<int> freq(26 * 26, 0);
        
        // Fill the frequency array
        for (auto word : words) {
            int idx = (word[0] - 'a') * 26 + (word[1] - 'a');
            freq[idx]++;
        }

        bool centerUsed = false;
        int ans = 0;
        
        // Traverse the frequency array
        for (auto word : words) {
            int idx1 = (word[0] - 'a') * 26 + (word[1] - 'a');
            int idx2 = (word[1] - 'a') * 26 + (word[0] - 'a');
            
            // If the word is a palindrome (e.g., "aa")
            if (word[0] == word[1]) {
                if (freq[idx1] >= 2) {
                    ans += 4; // Add both occurrences
                    freq[idx1] -= 2;
                } else if (freq[idx1] == 1 && !centerUsed) {
                    ans += 2; // Use this as a center palindrome
                    freq[idx1]--;
                    centerUsed = true;
                }
            } 
            // If the word is not a palindrome (e.g., "ab" and "ba")
            else if (freq[idx1] > 0 && freq[idx2] > 0) {
                ans += 4; // Add both "ab" and "ba"
                freq[idx1]--;
                freq[idx2]--;
            }
        }

        return ans;
    }
};

172000