
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 {212Please respect copyright.PENANAPpsBfwjatJ
public int longestPalindrome(String[] words) {212Please respect copyright.PENANAnkTFAKoKoF
HashMap<String, Integer> count = new HashMap<String, Integer>();212Please respect copyright.PENANA1kYUI5HgW7
// Count the number of occurrences of each word using a hashmap212Please respect copyright.PENANAc1eSFpTlYW
for (String word : words) {212Please respect copyright.PENANA1u47bIRjIa
Integer countOfTheWord = count.get(word);212Please respect copyright.PENANAwW4ADUJZGX
if (countOfTheWord == null) {212Please respect copyright.PENANAK3VWogL5uz
count.put(word, 1);212Please respect copyright.PENANA26gsuzxUwI
} else {212Please respect copyright.PENANAf49gVtcGVG
count.put(word, countOfTheWord + 1);212Please respect copyright.PENANAZjnep79Vcj
}212Please respect copyright.PENANAnTqzFpolgK
}212Please respect copyright.PENANAnUHJSKqced
// 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 word212Please respect copyright.PENANA35Jo17K0Ol
int answer = 0;212Please respect copyright.PENANA2VyywNxN5Y
boolean central = false;212Please respect copyright.PENANAgVEMEOEgoE
for (Map.Entry<String, Integer> entry : count.entrySet()) {212Please respect copyright.PENANA04mDZLYhUB
String word = entry.getKey();212Please respect copyright.PENANAkZfeVoRRTF
int countOfTheWord = entry.getValue();212Please respect copyright.PENANA9aLDGmFO5w
// if the word is a palindrome212Please respect copyright.PENANAogyjqWbQLc
if (word.charAt(0) == word.charAt(1)) {212Please respect copyright.PENANAsdV9wIwmAP
if (countOfTheWord % 2 == 0) {212Please respect copyright.PENANAC6CkZ1PUmN
answer += countOfTheWord;212Please respect copyright.PENANAYkOx3HUVvK
} else {212Please respect copyright.PENANAm8Mkkumhwy
answer += countOfTheWord - 1;212Please respect copyright.PENANAObEiZdJSqG
central = true;212Please respect copyright.PENANAJ1H34pBMyi
}212Please respect copyright.PENANAbZMjE7QzAA
// consider a pair of non-palindrome words such that one is the reverse of another212Please respect copyright.PENANADI7kWHXzRk
} else if (word.charAt(0) < word.charAt(1)) {212Please respect copyright.PENANAy3RFBX1Wju
String reversedWord = "" + word.charAt(1) + word.charAt(0);212Please respect copyright.PENANAWp0r71GTxv
if (count.containsKey(reversedWord)) {212Please respect copyright.PENANAYMIPmyKa6D
answer += 2 * Math.min(countOfTheWord, count.get(reversedWord));212Please respect copyright.PENANAB4jCitDaU3
}212Please respect copyright.PENANAxu3Usil1Sw
}212Please respect copyright.PENANAh4i9arvyOu
}212Please respect copyright.PENANAGO6kVxtiBQ
if (central) {212Please respect copyright.PENANAWoBsihYsI0
answer++;212Please respect copyright.PENANA9wPWSrKGo8
}212Please respect copyright.PENANAUttH63TGCq
return 2 * answer;212Please respect copyright.PENANAo7r6YiNP1R
}212Please respect copyright.PENANA5Oqb12TLqj
}
2: A Two-Dimensional Array Approach
class Solution {212Please respect copyright.PENANA6sfmt2YGRK
public int longestPalindrome(String[] words) {212Please respect copyright.PENANAVEIlngQt4h
final int alphabetSize = 26;212Please respect copyright.PENANAC7ROHRCvdx
int[][] count = new int[alphabetSize][alphabetSize];212Please respect copyright.PENANAjMX4PlNRBe
// Count the number of occurrences of each word using a two-dimensional array. 212Please respect copyright.PENANAz1cbtJduHY
for (String word : words) {212Please respect copyright.PENANA72nRYdgSSv
count[word.charAt(0) - 'a'][word.charAt(1) - 'a']++;212Please respect copyright.PENANAYWQnx1BvIp
}212Please respect copyright.PENANAlBnM37N37y
// The answer will denote the number of words in the final string and the boolean variable central will denote whether we have a central word212Please respect copyright.PENANA1XNbEZHd6C
int answer = 0;212Please respect copyright.PENANAw9N735tLG0
boolean central = false;212Please respect copyright.PENANAvThj9cxkkk
for (int i = 0; i < alphabetSize; i++) {212Please respect copyright.PENANAF9fTpkEajA
if (count[i][i] % 2 == 0) {212Please respect copyright.PENANABWdYfptpNP
answer += count[i][i];212Please respect copyright.PENANAIpcjpGwSXx
} else {212Please respect copyright.PENANASJHqWMwwU8
answer += count[i][i] - 1;212Please respect copyright.PENANAu7Mb1T96Pg
central = true;212Please respect copyright.PENANABfrWcHMykC
}212Please respect copyright.PENANA0no5dibrBe
for (int j = i + 1; j < alphabetSize; j++) {212Please respect copyright.PENANAP6dYqZijMh
answer += 2 * Math.min(count[i][j], count[j][i]);212Please respect copyright.PENANAB10khUc3P0
}212Please respect copyright.PENANAdZRG0wsjBz
}212Please respect copyright.PENANAWGmPi7jXor
if (central) {212Please respect copyright.PENANA586YXNp3qt
answer++;212Please respect copyright.PENANAlpKbFvVwhA
}212Please respect copyright.PENANAxUo2QEXDDD
return 2 * answer;212Please respect copyright.PENANArWK0xZ2ei5
}212Please respect copyright.PENANA7CT8GEf0ZT
}
212Please respect copyright.PENANAwzU0DUWP6X