Longest Palindrome by Concatenating Two Letter Words

Medium

Description

Given an array of strings words where each string is 2 letters, return the longest palindrome that can be formed by concatenating words.

Examples

Input:words = ["lc","cl","gg"]
Output:6
Explanation:

lc + gg + cl = 6.

Input:words = ["ab","ty","gy","fg","lc"]
Output:0
Explanation:

Edge case returning zero.

Input:words = ["aa","bb","cc","dd"]
Output:8
Explanation:

All words are palindromes themselves. It is possible to use all of them: aa + bb + cc + dd = 8. When building a palindrome from pairs, identical letter words can all be placed in the middle.

Constraints

  • 1 ≤ words.length ≤ 10⁵

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!