2131. Longest Palindrome by Concatenating Two Letter Words

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2131

class Solution:
    def longestPalindrome(self, words: List[str]) -> int:
        cnt = Counter(words)
        res = x = 0
        for k, v in cnt.items():
            if k[0] == k[1]:
                x += v & 1
                res += v // 2 * 2 * 2
            else:
                res += min(v, cnt[k[::-1]]) * 2
        res += 2 if x else 0
        return res