#2131
Longest Palindrome by Concatenating Two Letter Words
specialist · 810 · lc medium +31 · verified · 53.5% accepted · 2,951 likes · top 45%
Description
You are given an array of strings words where every string consists of exactly two lowercase English letters.
Build the longest possible palindrome by selecting and concatenating some elements from words in any order. Each element may be used at most once.
Return the length of the longest palindrome you can create. If no palindrome can be built, return 0.
A palindrome reads the same forwards and backwards.
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".
Code
1
2
3