Longest Palindrome by Concatenating Two Letter Words - Complete Solution Guide
Longest Palindrome by Concatenating Two Letter Words is LeetCode problem 2131, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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"] O
Detailed Explanation
The problem asks us to find the length of the longest possible palindrome that can be created by concatenating pairs of two-letter words from a given input list of words. Each word can be used at most once. A palindrome is a string that reads the same forwards and backward. The input consists of strings, each containing exactly two lowercase English letters. The output is an integer representing the maximum length of the palindrome.
Solution Approach
The solution uses a hash map (Counter in Python, HashMap in Java and C++, a custom HashTable in C) to count the occurrences of each word. It then iterates through the hash map, looking for words that are the reverse of other words. For each pair of words and their reverses, it takes the minimum count of the two words and multiplies it by 4 (since each pair contributes 2 words of length 2 each). It also keeps track of whether a word that is its own reverse exists with an odd number of occurrences. If so, it includes a central palindrome word of length 2 in the final palindrome, adding 2 to the total length.
Step-by-Step Algorithm
- Step 1: Create a hash map to store the frequency of each word in the input list.
- Step 2: Initialize a variable `length` to 0, which will store the length of the longest palindrome, and a boolean variable `center_present` to `false` to indicate whether a self-reversing word with an odd count is present.
- Step 3: Iterate through the hash map.
- Step 4: For each word in the hash map, check if the word is its own reverse (word[0] == word[1]).
- Step 5: If the word is its own reverse, add (count // 2) * 4 to `length`. If count is odd, set `center_present` to `true`.
- Step 6: If the word is not its own reverse, and if the first character is less than the second, reverse the word and check if its reverse exists in the hash map.
- Step 7: If the reverse exists, add min(count, reverse_count) * 4 to `length`.
- Step 8: After iterating through the hash map, if `center_present` is `true`, add 2 to `length` (for the central self-reversing word).
- Step 9: Return `length`.
Key Insights
- Insight 1: Recognize that a palindrome can be constructed by pairing words with their reverses (e.g., "lc" and "cl").
- Insight 2: Words that are their own reverses (e.g., "gg", "aa") can be used as the central part of the palindrome, but only one such word can be placed in the very center to maximize length.
- Insight 3: We can use a hash map (or similar data structure) to efficiently count the occurrences of each word.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table, String, Greedy, Counting.
Companies
Asked at: Databricks.