Skip to content

2237. Longest Palindrome By Concatenating Two Letter Words

Difficulty: Medium

LeetCode Problem View on GitHub


2237. Longest Palindrome by Concatenating Two Letter Words

Medium


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.

Solution

class Solution {
    public int longestPalindrome(String[] arr) {
        int n = arr.length;
        HashMap<String, Integer> map = new HashMap<>();
        int count = 0;
        for (int i = 0; i < n; i++) {
            String current = arr[i];
            if (current.charAt(0) == current.charAt(1)) {
                map.put(current, map.getOrDefault(current, 0) + 1);
                continue;
            }
            StringBuilder sb = new StringBuilder(current);
            sb.reverse();
            if (map.getOrDefault(sb.toString(), 0) > 0) {
                count += 4;
                map.put(sb.toString(), map.getOrDefault(sb.toString(), 0) -1);
            }
            else map.put(current, map.getOrDefault(current, 0) + 1);
        }
        HashMap<String, Integer> newMap = new HashMap<>();
        for (Map.Entry<String, Integer> curr : map.entrySet()) {
            String key = curr.getKey();
            if (key.charAt(0) == key.charAt(1)) {
                newMap.put(key, curr.getValue());
            }
        }
        int maxi = 0, odd = 0;
        for (Map.Entry<String, Integer> curr : newMap.entrySet()) {
            int val = curr.getValue();
            if (val % 2 == 0 && val != 0) count += val * 2;
            if (val % 2 == 1) {
                odd = 1;
                maxi = Math.max(maxi, val);
                count += (val - 1) * 2;
            }
        }
        if (odd > 0) count += 2;
        return count;
    }
}

Complexity Analysis

  • Time Complexity: O(?)
  • Space Complexity: O(?)

Approach

Detailed explanation of the approach will be added here