<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:
    def longestPalindrome(self, words: List[str]) -> int:
        result = 0
        potential_score = 0
        max_potential = 0
        cnt = Counter(words)
        add_two = False
        unlock_potential = True
        print(cnt)
        for word in cnt:
            inverse = word[::-1]
            word_occurences = cnt[word]
            inverse_occurences = cnt[inverse]
            if word_occurences == 0 or inverse_occurences == 0: 
                continue
            num_in_common = min(word_occurences, inverse_occurences)
            if num_in_common == 0:
                num_in_common = word_occurences
            num_to_score = num_in_common
            if word[0] != word[1]:
                result += num_to_score * 4
                unlock_potential = True
                print(f"adding {num_to_score * 4}")
                cnt[word] = 0
                cnt[inverse] = 0
            else:
                remainder =  word_occurences % 2
                if remainder:
                    print(word_occurences, remainder)
                    num_to_score = word_occurences - 1
                    add_two = True
                else:
                    num_to_score = word_occurences
                score_to_add = num_to_score * 2
                if score_to_add > max_potential:
                    max_potential = score_to_add
                potential_score += score_to_add
                print(potential_score)
                cnt[word] -= num_to_score
                
        if add_two:
            print("yes")
            result += 2
            result += potential_score
        elif unlock_potential:
            result += potential_score
        if result == 0:
            result += max_potential
        return result
            
1046