
Q: 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.
A: 1. A Hash Map Approach
class Solution {197Please respect copyright.PENANARMrw8tqqe1
public int longestPalindrome(String[] words) {197Please respect copyright.PENANAdqRZJxEUwc
HashMap<String, Integer> count = new HashMap<String, Integer>();197Please respect copyright.PENANAnZN8xvE6Qp
// Count the number of occurrences of each word using a hashmap197Please respect copyright.PENANA4Z1MJ08aG9
for (String word : words) {197Please respect copyright.PENANADVtJXHa44Z
Integer countOfTheWord = count.get(word);197Please respect copyright.PENANAHETXmf7IKT
if (countOfTheWord == null) {197Please respect copyright.PENANAZtgVMDqSVr
count.put(word, 1);197Please respect copyright.PENANA0RpjMoBpy3
} else {197Please respect copyright.PENANAsVMIXQYib4
count.put(word, countOfTheWord + 1);197Please respect copyright.PENANAICdiZfmbsE
}197Please respect copyright.PENANA7lZdcRss5q
}197Please respect copyright.PENANAPviqMOWdtC
// Initialize answer=0, central = false. The answer will denote the number of words in the final string and the boolean variable central will denote whether we have a central word197Please respect copyright.PENANAAvmUDfCbjM
int answer = 0;197Please respect copyright.PENANAqfn5ZwakII
boolean central = false;197Please respect copyright.PENANAnDmThAMPIJ
for (Map.Entry<String, Integer> entry : count.entrySet()) {197Please respect copyright.PENANAn4qHenNjOT
String word = entry.getKey();197Please respect copyright.PENANATG6BrOpCw6
int countOfTheWord = entry.getValue();197Please respect copyright.PENANAQfnGDmnvQR
// if the word is a palindrome197Please respect copyright.PENANAFbvPqn1iOu
if (word.charAt(0) == word.charAt(1)) {197Please respect copyright.PENANAf9xzZgQ7gs
if (countOfTheWord % 2 == 0) {197Please respect copyright.PENANAm27rmJZgix
answer += countOfTheWord;197Please respect copyright.PENANABKFyM4m4U8
} else {197Please respect copyright.PENANANDi3sMjXUU
answer += countOfTheWord - 1;197Please respect copyright.PENANA7bGEHDpFLc
central = true;197Please respect copyright.PENANAOZroEs9DZo
}197Please respect copyright.PENANAC09DmBvGHr
// consider a pair of non-palindrome words such that one is the reverse of another197Please respect copyright.PENANAxEwmWHFH0t
} else if (word.charAt(0) < word.charAt(1)) {197Please respect copyright.PENANAh1hnjyw08I
String reversedWord = "" + word.charAt(1) + word.charAt(0);197Please respect copyright.PENANAcLhbaHfd12
if (count.containsKey(reversedWord)) {197Please respect copyright.PENANAq0a2nddfGo
answer += 2 * Math.min(countOfTheWord, count.get(reversedWord));197Please respect copyright.PENANAxAg1vFIXxF
}197Please respect copyright.PENANA7RgtXIgY8m
}197Please respect copyright.PENANAwyi8Xhl2kM
}197Please respect copyright.PENANACJfq9fXB8G
if (central) {197Please respect copyright.PENANAORzOEyuLwq
answer++;197Please respect copyright.PENANAd1A5tdkOUz
}197Please respect copyright.PENANAzg85wzhorH
return 2 * answer;197Please respect copyright.PENANA9hARVHBgN1
}197Please respect copyright.PENANAjg46DPeZTb
}
2: A Two-Dimensional Array Approach
class Solution {197Please respect copyright.PENANAaBNzWGhXVL
public int longestPalindrome(String[] words) {197Please respect copyright.PENANASfeseiNqVy
final int alphabetSize = 26;197Please respect copyright.PENANA5m5yMq0tnH
int[][] count = new int[alphabetSize][alphabetSize];197Please respect copyright.PENANAHxmSausNz5
// Count the number of occurrences of each word using a two-dimensional array. 197Please respect copyright.PENANAXaZCyg2dnz
for (String word : words) {197Please respect copyright.PENANAQIxI94qndb
count[word.charAt(0) - 'a'][word.charAt(1) - 'a']++;197Please respect copyright.PENANA0OW3IXKDvv
}197Please respect copyright.PENANA0K4yS5EIlv
// The answer will denote the number of words in the final string and the boolean variable central will denote whether we have a central word197Please respect copyright.PENANAunuEgbu1Fo
int answer = 0;197Please respect copyright.PENANApID0pFEQRR
boolean central = false;197Please respect copyright.PENANATZkuX1Zb3q
for (int i = 0; i < alphabetSize; i++) {197Please respect copyright.PENANAJoqJbZH9ok
if (count[i][i] % 2 == 0) {197Please respect copyright.PENANADPVv5q3WSn
answer += count[i][i];197Please respect copyright.PENANAc1xaqaJ5ED
} else {197Please respect copyright.PENANAnkrbkLCEMU
answer += count[i][i] - 1;197Please respect copyright.PENANAQusFrT33RS
central = true;197Please respect copyright.PENANATeOJkUM51L
}197Please respect copyright.PENANAZIGe5Dl1le
for (int j = i + 1; j < alphabetSize; j++) {197Please respect copyright.PENANAUgNWPBbMgn
answer += 2 * Math.min(count[i][j], count[j][i]);197Please respect copyright.PENANATWeWKSep7E
}197Please respect copyright.PENANAy5VWr9JCH7
}197Please respect copyright.PENANAMAnSYvGtYS
if (central) {197Please respect copyright.PENANAEoUCKpxS72
answer++;197Please respect copyright.PENANAjz9h4GwcKS
}197Please respect copyright.PENANAaxviTXNWmW
return 2 * answer;197Please respect copyright.PENANA2K0Aq2Iwq3
}197Please respect copyright.PENANA6D34KDQX72
}
197Please respect copyright.PENANAXANIL6tkCc