Imagine you have a collection of two-letter word tiles and you want to create the longest possible palindrome by arranging them strategically!
You are given an array of strings words where each string consists of exactly two lowercase English letters. Your goal is to select some of these words and concatenate them in any order to form the longest possible palindrome.
Key Rules:
- Each word can be used at most once
- You can arrange the selected words in any order
- The final string must read the same forwards and backwards
Example: Given ["lc","cl","gg"], you can create "lcggcl" which reads the same forwards and backwards, giving a length of 6.
Return the length of the longest palindrome you can create, or 0 if impossible.
Input & Output
Visualization
Time & Space Complexity
Single pass through words array to count frequencies, then O(1) processing per unique word
Hash map stores at most n unique words in worst case
Constraints
- 1 โค words.length โค 105
- words[i].length == 2
- words[i] consists of lowercase English letters only
- Each word can be used at most once