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 {143Please respect copyright.PENANAkpff22YPRG
public int longestPalindrome(String[] words) {143Please respect copyright.PENANAyYPwpqlpPP
HashMap<String, Integer> count = new HashMap<String, Integer>();143Please respect copyright.PENANAsZdu3YK4ei
// Count the number of occurrences of each word using a hashmap143Please respect copyright.PENANAAJqFoql1OX
for (String word : words) {143Please respect copyright.PENANARwN1Mjdn3M
Integer countOfTheWord = count.get(word);143Please respect copyright.PENANAWXSEJQXTUV
if (countOfTheWord == null) {143Please respect copyright.PENANAi28FsHlhaR
count.put(word, 1);143Please respect copyright.PENANAVuEymLONko
} else {143Please respect copyright.PENANAzwi2b7OCq6
count.put(word, countOfTheWord + 1);143Please respect copyright.PENANALhlgINlYow
}143Please respect copyright.PENANA3lb0wWR87d
}143Please respect copyright.PENANAU33Jxw8HtQ
// 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 word143Please respect copyright.PENANAf3SLnVLJEw
int answer = 0;143Please respect copyright.PENANALc2C2Y4rpT
boolean central = false;143Please respect copyright.PENANAM6hxxU0x6j
for (Map.Entry<String, Integer> entry : count.entrySet()) {143Please respect copyright.PENANAwLXvfStz7x
String word = entry.getKey();143Please respect copyright.PENANAF78vS7vEgH
int countOfTheWord = entry.getValue();143Please respect copyright.PENANAaHK2d1tfW7
// if the word is a palindrome143Please respect copyright.PENANAeIy73fRmVm
if (word.charAt(0) == word.charAt(1)) {143Please respect copyright.PENANAnKQvJtgfb1
if (countOfTheWord % 2 == 0) {143Please respect copyright.PENANAhkxkXcVpVt
answer += countOfTheWord;143Please respect copyright.PENANAOGiJcGi9en
} else {143Please respect copyright.PENANA961emUABDs
answer += countOfTheWord - 1;143Please respect copyright.PENANA2ERf158w4G
central = true;143Please respect copyright.PENANAnAagtHaEXN
}143Please respect copyright.PENANApbD8Ilo7cG
// consider a pair of non-palindrome words such that one is the reverse of another143Please respect copyright.PENANAYDIw9pGgaq
} else if (word.charAt(0) < word.charAt(1)) {143Please respect copyright.PENANAuXk9DYUVKR
String reversedWord = "" + word.charAt(1) + word.charAt(0);143Please respect copyright.PENANAY9ydo9A0nt
if (count.containsKey(reversedWord)) {143Please respect copyright.PENANAyfN0qSWb5A
answer += 2 * Math.min(countOfTheWord, count.get(reversedWord));143Please respect copyright.PENANAWily9W0btB
}143Please respect copyright.PENANA7Lrv56kxm1
}143Please respect copyright.PENANAhtJHQNNM6z
}143Please respect copyright.PENANAxfSHTcuQRr
if (central) {143Please respect copyright.PENANActAyPYByX4
answer++;143Please respect copyright.PENANAv4Rq2OdcTw
}143Please respect copyright.PENANASB45OgbD87
return 2 * answer;143Please respect copyright.PENANAIZlHZgL8oE
}143Please respect copyright.PENANALFRASr3iOo
}
2: A Two-Dimensional Array Approach
class Solution {143Please respect copyright.PENANAeaUV2GO1Gr
public int longestPalindrome(String[] words) {143Please respect copyright.PENANA8vL7qn5TXV
final int alphabetSize = 26;143Please respect copyright.PENANA60OrWFjOC2
int[][] count = new int[alphabetSize][alphabetSize];143Please respect copyright.PENANAj51OsqgIXU
// Count the number of occurrences of each word using a two-dimensional array. 143Please respect copyright.PENANAz1vtQsy5VQ
for (String word : words) {143Please respect copyright.PENANAw6Ti6BIACp
count[word.charAt(0) - 'a'][word.charAt(1) - 'a']++;143Please respect copyright.PENANAhoCecXgcu5
}143Please respect copyright.PENANAmIOVzhQxNF
// The answer will denote the number of words in the final string and the boolean variable central will denote whether we have a central word143Please respect copyright.PENANAVuz6biN1gY
int answer = 0;143Please respect copyright.PENANAIE9b1nmfIZ
boolean central = false;143Please respect copyright.PENANAp7ChMJZk1j
for (int i = 0; i < alphabetSize; i++) {143Please respect copyright.PENANA0ddQqINNXZ
if (count[i][i] % 2 == 0) {143Please respect copyright.PENANAcVB7k7Uny1
answer += count[i][i];143Please respect copyright.PENANAL4ogjYw2E5
} else {143Please respect copyright.PENANAGQvfLy3Dy6
answer += count[i][i] - 1;143Please respect copyright.PENANA4yEjtlosDN
central = true;143Please respect copyright.PENANACJUfZHyT9r
}143Please respect copyright.PENANAWfGnt6cy5s
for (int j = i + 1; j < alphabetSize; j++) {143Please respect copyright.PENANAt9rk4suNGF
answer += 2 * Math.min(count[i][j], count[j][i]);143Please respect copyright.PENANA1yKPhj5nFN
}143Please respect copyright.PENANABGG8ILNt6J
}143Please respect copyright.PENANANbMFXo3zlg
if (central) {143Please respect copyright.PENANAZo5bvHTkJr
answer++;143Please respect copyright.PENANA8Kzgnl2fTo
}143Please respect copyright.PENANAyCQtKSbiEY
return 2 * answer;143Please respect copyright.PENANAG008ed7QWc
}143Please respect copyright.PENANAAlQ752weo5
}
143Please respect copyright.PENANAtFH56mjNCS