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.
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".
Constraints:
1 <= words.length <= 105words[i].length == 2words[i] consists of lowercase English letters.The first approach involves counting occurrences of each word and its reverse. If a word appears with its reverse, they can be directly paired to contribute to the palindrome length. Symmetrical words like 'gg' can be used optimally to form palindromes, with one potentially serving as a center if their count is odd.
The C solution uses a manual structure to count word occurrences and then utilizes sorting and dual iterations to pair words and compute maximum palindrome length. Reversals are computed for each word, ensuring that all possible pairs contribute optimally to the palindrome length.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n log n) due to sorting.
Space Complexity: O(n) for storage of words and their counts.
This approach involves creating a hash map to quickly check for the existence of a word's reverse in the input, allowing us to efficiently pair words or use symmetric words optimally. We calculate pairs and handle middle contributions by taking account of unsused symmetric words.
This C solution employs a two-dimensional array approach (map) to store the frequency of character pairs. It counts how many of each type of word pair exist and checks for their reversal, adding contributions to the length based on pair formation. It separately checks for symmetrical pairs to add potential middle characters.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n) with a constant factor given by alphabets.
Space Complexity: O(1) as the 26x26 map is constant in size.
| Approach | Complexity |
|---|---|
| Greedy Pair Matching | Time Complexity: O(n log n) due to sorting. |
| String Reversal and Hash | Time Complexity: O(n) with a constant factor given by alphabets. |
Longest Palindrome by Concatenating Two Letter Words || LeetCode 2131 || Biweekly LeetCode-69 || • Bro Coders • 3,209 views views
Watch 9 more video solutions →Practice Longest Palindrome by Concatenating Two Letter Words with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor