Longest Palindrome by Concatenating Two Letter Words

Difficulty: Medium

Topics: Array, Hash Table, String, Greedy, Counting

Introduction

In this tutorial, we will explore a fascinating problem: creating the longest possible palindrome by concatenating two-letter words from a given array. A palindrome is a string that reads the same forwards and backwards, such as “abba” or “cc”. Our goal is to determine the maximum length of such a palindrome using the provided words.

Prerequisites

  • Basic understanding of strings and arrays
  • Familiarity with hash tables and counting techniques
  • Basic knowledge of PHP programming

Problem Statement

You are given an array of strings words. Each element of words consists of two lowercase English letters. Your task is to 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.

Examples

Example 1:

  • Input: words = [“lc”,”cl”,”gg”]
  • Output: 6
  • Explanation: One longest palindrome is “lc” + “gg” + “cl” = “lcggcl”, of length 6.

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.

Example 3:

  • Input: words = [“cc”,”ll”,”xx”]
  • Output: 2
  • Explanation: One longest palindrome is “cc”, of length 2.

Constraints

  • 1 <= words.length <= 10^5
  • words[i].length == 2
  • words[i] consists of lowercase English letters.

Hints

  1. A palindrome must be mirrored over the center. If we have a palindrome and prepend the word “ab” on the left, we must append “ba” on the right to keep it a palindrome.
  2. The number of times we can do this is the minimum of the occurrences of “ab” and “ba”.
  3. For words that are already palindromes (e.g., “aa”), we can use pairs of these words symmetrically around the center. If there’s an odd count of such words, one can be placed in the center of the palindrome.

Approach

To solve this problem, we will follow these steps:

  1. Count Frequencies: First, we count the frequency of each word using a hash map.
  2. Handle Palindromic Words: For words that are palindromes themselves (e.g., “aa”), we can use pairs of these words symmetrically around the center. If there’s an odd count of such words, one can be placed in the center of the palindrome.
  3. Handle Non-Palindromic Pairs: For non-palindromic words (e.g., “ab” and “ba”), we check if their reverse exists in the list. Each valid pair contributes to the palindrome symmetrically.

Implementation

Let’s implement this solution in PHP:

<?php
/**
 * @param String[] $words
 * @return Integer
 */
function longestPalindrome($words) {
    // Implementation goes here
}

// Example Test Cases
$test1 = ["lc", "cl", "gg"];
$test2 = ["ab", "ty", "yt", "lc", "cl", "ab"];
$test3 = ["cc", "ll", "xx"];

echo longestPalindrome($test1) . "\n"; // Output: 6
echo longestPalindrome($test2) . "\n"; // Output: 8
echo longestPalindrome($test3) . "\n"; // Output: 2
?>

Explanation

  1. Frequency Counting: We first count how many times each word appears in the list using a hash map.
  2. Processing Palindromic Words: For each palindromic word (e.g., “aa”), we determine how many pairs can be formed. Each pair contributes 4 characters to the palindrome. If there’s an odd count of such words, we can place one in the center.
  3. Processing Non-Palindromic Pairs: For each non-palindromic word, we check if its reverse exists. If it does, we use the minimum count of the word and its reverse to form pairs, each contributing 4 characters. We ensure each pair is processed only once by checking lexicographical order.
  4. Center Adjustment: Finally, if there’s any palindromic word with an odd count, we can place one in the center, adding 2 characters to the total length.

This approach efficiently counts and pairs words to maximize the length of the resulting palindrome, ensuring optimal performance even for large input sizes.

Contact Links

If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

Sources:

Source: Original Article