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
Longest Palindrome by Concatenating Two Letter Words INPUT words array: "lc" "cl" "gg" [0] [1] [2] Word Types: Mirror Pairs "lc" <--> "cl" Symmetric Word "gg" (same reversed) Input: ["lc","cl","gg"] ALGORITHM STEPS 1 Count Words Use HashMap to count each word frequency 2 Find Mirror Pairs For "lc", reverse = "cl" min(count) pairs = 1 3 Handle Symmetric "gg" can go in center Add 2 if odd count 4 Calculate Length pairs*4 + center*2 = 1*4 + 1*2 = 6 Building Palindrome: lc gg cl "lc" + "gg" + "cl" Length = 2 + 2 + 2 = 6 FINAL RESULT Constructed Palindrome: l c g g c l mirrors Palindrome Check: OK lcggcl == lcggcl (reversed) Output: 6 Maximum palindrome length using given words Key Insight: For palindrome: non-symmetric words need their reverse (e.g., "lc" needs "cl") -- use both sides. Symmetric words (e.g., "gg", "aa") can pair with themselves OR one can be placed in the center. Time: O(n), Space: O(n) using HashMap to track word frequencies and find matching pairs. TutorialsPoint - Longest Palindrome by Concatenating Two Letter Words | Optimal Solution
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