2131. Longest Palindrome by Concatenating Two Letter Words LeetCode Solution

In this guide, you will get 2131. Longest Palindrome by Concatenating Two Letter Words LeetCode Solution with the best time and space complexity. The solution to Longest Palindrome by Concatenating Two Letter Words problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Longest Palindrome by Concatenating Two Letter Words solution in C++
  4. Longest Palindrome by Concatenating Two Letter Words solution in Java
  5. Longest Palindrome by Concatenating Two Letter Words solution in Python
  6. Additional Resources
2131. Longest Palindrome by Concatenating Two Letter Words LeetCode Solution image

Problem Statement of Longest Palindrome by Concatenating Two Letter Words

You are given an array of strings words. Each element of words consists of two lowercase English letters.
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.
A palindrome is a string that reads the same forward and backward.

Example 1:

Input: words = [“lc”,”cl”,”gg”]
Output: 6
Explanation: One longest palindrome is “lc” + “gg” + “cl” = “lcggcl”, of length 6.
Note that “clgglc” is another longest palindrome that can be created.

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.
Note that “lcyttycl” is another longest palindrome that can be created.

Example 3:

Input: words = [“cc”,”ll”,”xx”]
Output: 2
Explanation: One longest palindrome is “cc”, of length 2.
Note that “ll” is another longest palindrome that can be created, and so is “xx”.

Constraints:

1 <= words.length <= 105
words[i].length == 2
words[i] consists of lowercase English letters.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(26^2) = O(1)

2131. Longest Palindrome by Concatenating Two Letter Words LeetCode Solution in C++

class Solution {
 public:
  int longestPalindrome(vector<string>& words) {
    int ans = 0;
    vector<vector<int>> count(26, vector<int>(26));

    for (const string& word : words) {
      const int i = word[0] - 'a';
      const int j = word[1] - 'a';
      if (count[j][i]) {
        ans += 4;
        --count[j][i];
      } else {
        ++count[i][j];
      }
    }

    for (int i = 0; i < 26; ++i)
      if (count[i][i])
        return ans + 2;

    return ans;
  }
};
/* code provided by PROGIEZ */

2131. Longest Palindrome by Concatenating Two Letter Words LeetCode Solution in Java

class Solution {
  public int longestPalindrome(String[] words) {
    int ans = 0;
    int[][] count = new int[26][26];

    for (final String word : words) {
      final int i = word.charAt(0) - 'a';
      final int j = word.charAt(1) - 'a';
      if (count[j][i] > 0) {
        ans += 4;
        --count[j][i];
      } else {
        ++count[i][j];
      }
    }

    for (int i = 0; i < 26; ++i)
      if (count[i][i] > 0)
        return ans + 2;

    return ans;
  }
}
// code provided by PROGIEZ

2131. Longest Palindrome by Concatenating Two Letter Words LeetCode Solution in Python

class Solution:
  def longestPalindrome(self, words: list[str]) -> int:
    ans = 0
    count = [[0] * 26 for _ in range(26)]

    for a, b in words:
      i = ord(a) - ord('a')
      j = ord(b) - ord('a')
      if count[j][i]:
        ans += 4
        count[j][i] -= 1
      else:
        count[i][j] += 1

    for i in range(26):
      if count[i][i]:
        return ans + 2

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.