<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) {
        std::ios_base::sync_with_stdio(false);
        std::cin.tie(nullptr);
        sort(words.begin(),words.end());
        unordered_map<string,int> mpp;
        int cnt = 0;

        // string temp = "";
        // string taken = "";
        // for(string s : words){
        //     if(mpp.find(s)==mpp.end()){
        //         temp = s;
        //         sort(temp.begin(),temp.end());
        //         if(mpp.find(temp)!=mpp.end()) {
        //             cnt += 2;
        //             mpp[temp]--;
        //             if(mpp[temp]==0) mpp.erase(temp);
        //             continue;
        //         }
        //     }
        //     mpp[s]++;
        //     if((s[0]==s[1]) && (mpp[s]>1 || taken.size()==0)) {
        //         cnt++;
        //         if(mpp[s]==1) taken = s;
        //         if((s==taken) && (mpp[s]>1)) taken = "";
        //     }
        // }

        for(auto s : words){
            mpp[s]++;
        }
        // string temp = "";
        bool taken = 0;
        for(auto s : mpp){
            if(s.first[0]<s.first[1]) continue;
            else if(s.first[0]==s.first[1]){
                if(s.second%2==0){
                    cnt += s.second;
                }
                else{
                    if(!taken){
                        cnt += s.second;
                        taken = 1;
                    }
                    else{
                        cnt += (s.second-1);
                    }
                }
            }
            else{
                // temp = s.first;
                // sort(temp.begin(),temp.end());
                string temp="";
                temp+=s.first[1];
                temp+=s.first[0];
                if(mpp.find(temp)!=mpp.end()){
                    int occurences1 = mpp[temp];
                    int occurences2 = s.second;
                    int mini = min(occurences1,occurences2);
                    cnt += (mini*2);
                }
            }
        }
        return cnt*2;
    }
};

auto init = []() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    return 'c';
}();
171200