Skip to content

2131. Longest Palindrome by Concatenating Two Letter Words #1725

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to determine the longest possible palindrome that can be formed by concatenating selected two-letter words from a given list. Each word can be used at most once. The approach involves efficiently counting word frequencies and strategically using pairs of words to form the palindrome.

Approach

  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")…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim May 25, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants