Search⌘ K
AI Features

Longest Palindrome by Concatenating Two-Letter Words

Explore strategies to solve the longest palindrome problem involving two-letter words. Understand how to track word frequencies and apply frequency analysis to build palindromes efficiently. This lesson guides you through the problem requirements and helps develop coding solutions that handle constraints and optimize palindrome length.

Statement

Suppose you are given an array of strings, words, and each element in the array has a length of two. You need to return the length of the longest palindrome that can be made by concatenating some elements from words. If no palindrome can be made, return 0.

A palindrome is a sequence of characters that reads the same forward and backward.

Constraints:

  • 11 \leq words.length 1000\leq 1000
  • words[i].length =2= 2
  • Each word can be used at most once.
  • words should only consist of lowercase English letters.

Examples

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Longest Palindrome by Concatenating Two-Letter Words

1.

What is the length of the longest palindrome that can be constructed from the words in the input array below?

words = [“ab”, “cd”, “ef”, “gh”, “ij”]

A.

00

B.

44

C.

66

D.

88


1 / 4

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5

Try it yourself

Implement your solution in the following coding playground.

C++
usercode > main.cpp
int LongestPalindrome(std::vector<std::string>& words) {
// Replace this placeholder return statement with your code
return -1;
}
Longest Palindrome by Concatenating Two-Letter Words