Search⌘ K
AI Features

Longest Palindrome by Concatenating Two-Letter Words

Understand how to solve the problem of finding the longest palindrome by concatenating two-letter words from an array. Learn to track and combine elements effectively to form palindromes, applying these skills to common coding challenges.

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.

JavaScript
usercode > main.js
function longestPalindrome(words) {
// Replace this placeholder return statement with your code
return -1
}
export {
longestPalindrome
}
Longest Palindrome by Concatenating Two-Letter Words