Longest Palindrome by Concatenating Two Letter Words - Problem

You are given an array of strings words. Each element of words consists of two lowercase English letters.

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

Return the length of the longest palindrome that you can create. If it is impossible to create any palindrome, return 0.

A palindrome is a string that reads the same forward and backward.

Input & Output

Example 1 — Basic Mixed Words
$ Input: words = ["lc","cl","gg"]
Output: 6
💡 Note: We can form palindrome "gglccllcgg" but since we can use each word at most once, the best we can do is "lccl" using "lc" and "cl" for length 4, or "gg" + something. Actually, we can use all three: "gglccl" isn't palindrome, but "lccl" (length 4) + "gg" separately isn't optimal. The optimal is "ggllcc" rearranged as "gg" + "cl" + "lc" = "ggcllc" which isn't palindrome. Let me recalculate: "cl" + "lc" = "cllc" (length 4) + "gg" unused, or "gg" as center with "cl"+"lc" around it somehow. The answer should be 6 for "lcclgggcllcl" but that uses words multiple times. Actually: "cl"+"gg"+"lc" can form "clgglc" which isn't palindrome. The correct approach: we can form "gglccllcgg" conceptually, but limited to one use each. We get: pair(cl,lc)=4 chars + gg can go in center = 6 total: "lc"+"gg"+"cl" = "cllcgg" rearranged as palindrome "clggcl"? No. Let me think systematically: cl↔lc (reverse pair) = 4 chars, gg↔gg (self pair, use 1) = 2 chars. Total = 6.
Example 2 — Multiple Pairs
$ Input: words = ["ab","ty","yt","lc","cl","ab"]
Output: 8
💡 Note: We have pairs: ab↔? (ab appears twice, no ba), ty↔yt (1 pair = 4 chars), lc↔cl (1 pair = 4 chars). Total = 4 + 4 = 8 chars. The ab's can't pair with anything so they're unused.
Example 3 — Only Self-Palindromes
$ Input: words = ["cc","ll","xx"]
Output: 2
💡 Note: All words are self-palindromes (cc, ll, xx). We can use only one as center since each appears once. Maximum length = 2 (using any one word).

Constraints

  • 1 ≤ words.length ≤ 105
  • words[i].length == 2
  • words[i] consists of lowercase English letters

Visualization

Tap to expand
Building Longest Palindrome from Two-Letter WordsInput:lcclggStep 1: Identify pairs and self-palindromeslc ↔ clgg (self)Pair: 4 charsCenter: 2 charsStep 2: Arrange as palindrome structurelcggcl"lc" + "gg" + "cl" → "lcggcl"Result: Length 6✅ Palindrome reads same forward and backward
Understanding the Visualization
1
Input Words
Array of two-letter words to choose from
2
Find Pairs
Match words with their reverses plus self-palindromes
3
Build Palindrome
Arrange pairs symmetrically with optional center
Key Takeaway
🎯 Key Insight: Palindromes need symmetric word pairs plus at most one center self-palindrome
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
23.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen