
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 {249Please respect copyright.PENANATKZPtXBQJI
public int longestPalindrome(String[] words) {249Please respect copyright.PENANA6Rt5PPlABe
HashMap<String, Integer> count = new HashMap<String, Integer>();249Please respect copyright.PENANAcKTKBJ02TJ
// Count the number of occurrences of each word using a hashmap249Please respect copyright.PENANAu13YGcrEeX
for (String word : words) {249Please respect copyright.PENANAjszaEMYQCr
Integer countOfTheWord = count.get(word);249Please respect copyright.PENANAb86jfGKPb2
if (countOfTheWord == null) {249Please respect copyright.PENANAsHyKqQLJbV
count.put(word, 1);249Please respect copyright.PENANAscQpcdjAQF
} else {249Please respect copyright.PENANAZnKEmLIG23
count.put(word, countOfTheWord + 1);249Please respect copyright.PENANAheGt6liznz
}249Please respect copyright.PENANAomg1JTPyrv
}249Please respect copyright.PENANAqICf0gV7W6
// 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 word249Please respect copyright.PENANAILGHDmNE3A
int answer = 0;249Please respect copyright.PENANAtugLuAPzKB
boolean central = false;249Please respect copyright.PENANAV4LGBy4Jqh
for (Map.Entry<String, Integer> entry : count.entrySet()) {249Please respect copyright.PENANAmDhiHHMU1T
String word = entry.getKey();249Please respect copyright.PENANAGHGSdVVX8o
int countOfTheWord = entry.getValue();249Please respect copyright.PENANAXL6vxwPUqP
// if the word is a palindrome249Please respect copyright.PENANArzaWMgmA9k
if (word.charAt(0) == word.charAt(1)) {249Please respect copyright.PENANAQvEV9d8Brs
if (countOfTheWord % 2 == 0) {249Please respect copyright.PENANAbGDH9QfOsR
answer += countOfTheWord;249Please respect copyright.PENANAN8bsjzjP9S
} else {249Please respect copyright.PENANAGr9sLJf7vY
answer += countOfTheWord - 1;249Please respect copyright.PENANAlHcfN3r45q
central = true;249Please respect copyright.PENANAddBuxF75DV
}249Please respect copyright.PENANAaVJsCkGt7m
// consider a pair of non-palindrome words such that one is the reverse of another249Please respect copyright.PENANAXdEgSMHMz6
} else if (word.charAt(0) < word.charAt(1)) {249Please respect copyright.PENANA2jeviK9IwN
String reversedWord = "" + word.charAt(1) + word.charAt(0);249Please respect copyright.PENANAXUNHI9xJXM
if (count.containsKey(reversedWord)) {249Please respect copyright.PENANArrBqXb7SXA
answer += 2 * Math.min(countOfTheWord, count.get(reversedWord));249Please respect copyright.PENANAXcZI5lBTJy
}249Please respect copyright.PENANAVe6LswfVx6
}249Please respect copyright.PENANAXDL17G2LL7
}249Please respect copyright.PENANAr3jNpDa3gi
if (central) {249Please respect copyright.PENANARntPn1gH2t
answer++;249Please respect copyright.PENANAumL1LReCYj
}249Please respect copyright.PENANAw8JakFkNNQ
return 2 * answer;249Please respect copyright.PENANA4iIGa9KcnJ
}249Please respect copyright.PENANAJ2C8ZGkIuJ
}
2: A Two-Dimensional Array Approach
class Solution {249Please respect copyright.PENANAurATOFzl2B
public int longestPalindrome(String[] words) {249Please respect copyright.PENANAgwYU1F22fq
final int alphabetSize = 26;249Please respect copyright.PENANAESO9eYfhrz
int[][] count = new int[alphabetSize][alphabetSize];249Please respect copyright.PENANAFdrog0Kv6j
// Count the number of occurrences of each word using a two-dimensional array. 249Please respect copyright.PENANAo9GznLWOWK
for (String word : words) {249Please respect copyright.PENANAzXHm6Vozmp
count[word.charAt(0) - 'a'][word.charAt(1) - 'a']++;249Please respect copyright.PENANAODvYtTeLBW
}249Please respect copyright.PENANAB4dpGgLedr
// The answer will denote the number of words in the final string and the boolean variable central will denote whether we have a central word249Please respect copyright.PENANAX2GMwcPM4q
int answer = 0;249Please respect copyright.PENANAFxRoX6oJgn
boolean central = false;249Please respect copyright.PENANA7tO0dCykFs
for (int i = 0; i < alphabetSize; i++) {249Please respect copyright.PENANAP7KE8drHXd
if (count[i][i] % 2 == 0) {249Please respect copyright.PENANACJ7IXxkbcP
answer += count[i][i];249Please respect copyright.PENANAlOkqrnoHOE
} else {249Please respect copyright.PENANAzaXqtuj5rS
answer += count[i][i] - 1;249Please respect copyright.PENANAtQY1p4xrj3
central = true;249Please respect copyright.PENANA2hHm4ZPmIR
}249Please respect copyright.PENANAF3Q4W1tees
for (int j = i + 1; j < alphabetSize; j++) {249Please respect copyright.PENANAQ4RN5WMDwR
answer += 2 * Math.min(count[i][j], count[j][i]);249Please respect copyright.PENANA6jGPcvmJdm
}249Please respect copyright.PENANAb04sNfmsMl
}249Please respect copyright.PENANAZwt7zbHv8D
if (central) {249Please respect copyright.PENANA0GYmQaAHhv
answer++;249Please respect copyright.PENANAm5UB7epDkE
}249Please respect copyright.PENANA47EqDeIt6x
return 2 * answer;249Please respect copyright.PENANAXtDDaVeOgX
}249Please respect copyright.PENANA2edGq9kXzW
}
249Please respect copyright.PENANApQJ5BAy6vr