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 {124Please respect copyright.PENANAC4rJcP4wxn
public int longestPalindrome(String[] words) {124Please respect copyright.PENANAHc5Ltb2auA
HashMap<String, Integer> count = new HashMap<String, Integer>();124Please respect copyright.PENANABy2LI7B8SK
// Count the number of occurrences of each word using a hashmap124Please respect copyright.PENANAZMxub2UO0i
for (String word : words) {124Please respect copyright.PENANA3Lwo8SDEeV
Integer countOfTheWord = count.get(word);124Please respect copyright.PENANABLi1H92XIW
if (countOfTheWord == null) {124Please respect copyright.PENANAdoWZexD4KR
count.put(word, 1);124Please respect copyright.PENANAwYGtS25qaj
} else {124Please respect copyright.PENANAn6Hnq84fB1
count.put(word, countOfTheWord + 1);124Please respect copyright.PENANAmceW2wrmU9
}124Please respect copyright.PENANAdQuNn59Xzm
}124Please respect copyright.PENANA2WUkT7wXln
// 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 word124Please respect copyright.PENANAF9Sz6gGrE6
int answer = 0;124Please respect copyright.PENANAQqAHJJhU1B
boolean central = false;124Please respect copyright.PENANAmetVHdgthB
for (Map.Entry<String, Integer> entry : count.entrySet()) {124Please respect copyright.PENANA86gAdqbStO
String word = entry.getKey();124Please respect copyright.PENANABKaejm8W0H
int countOfTheWord = entry.getValue();124Please respect copyright.PENANAhyv6B6Ms4Y
// if the word is a palindrome124Please respect copyright.PENANACSAKN9YpAh
if (word.charAt(0) == word.charAt(1)) {124Please respect copyright.PENANAbLsqfGt3eh
if (countOfTheWord % 2 == 0) {124Please respect copyright.PENANA6pWCDvSuAP
answer += countOfTheWord;124Please respect copyright.PENANAKzSBwm6XaI
} else {124Please respect copyright.PENANAmB3OfWFF75
answer += countOfTheWord - 1;124Please respect copyright.PENANAMQ3OOMhY3T
central = true;124Please respect copyright.PENANAwWsHRAvuEB
}124Please respect copyright.PENANAuKuK05naGg
// consider a pair of non-palindrome words such that one is the reverse of another124Please respect copyright.PENANATw810vCkua
} else if (word.charAt(0) < word.charAt(1)) {124Please respect copyright.PENANAQYMjIpG4Ft
String reversedWord = "" + word.charAt(1) + word.charAt(0);124Please respect copyright.PENANAPNr45PYlLG
if (count.containsKey(reversedWord)) {124Please respect copyright.PENANAmVWGFa31lT
answer += 2 * Math.min(countOfTheWord, count.get(reversedWord));124Please respect copyright.PENANAYn1hEI2iRC
}124Please respect copyright.PENANACUDwdkObQk
}124Please respect copyright.PENANAV3jUGHtnqY
}124Please respect copyright.PENANAOrRlO2y6OY
if (central) {124Please respect copyright.PENANAkKLAPSSQm5
answer++;124Please respect copyright.PENANARLHMpZc1NY
}124Please respect copyright.PENANAWzkQiVQzJn
return 2 * answer;124Please respect copyright.PENANA5qyYQ7sBgk
}124Please respect copyright.PENANACFImOMR0qr
}
2: A Two-Dimensional Array Approach
class Solution {124Please respect copyright.PENANArZGPQWenHM
public int longestPalindrome(String[] words) {124Please respect copyright.PENANAVCrIVFhac0
final int alphabetSize = 26;124Please respect copyright.PENANAiA47gVkLez
int[][] count = new int[alphabetSize][alphabetSize];124Please respect copyright.PENANA95lRal4zE9
// Count the number of occurrences of each word using a two-dimensional array. 124Please respect copyright.PENANABMEXLW8T80
for (String word : words) {124Please respect copyright.PENANA9sR5w1gJ2y
count[word.charAt(0) - 'a'][word.charAt(1) - 'a']++;124Please respect copyright.PENANAl5HQHLfRkG
}124Please respect copyright.PENANAVRAsC0J30R
// The answer will denote the number of words in the final string and the boolean variable central will denote whether we have a central word124Please respect copyright.PENANABnbr7diZON
int answer = 0;124Please respect copyright.PENANAe0a0nyaSc0
boolean central = false;124Please respect copyright.PENANAqouzkp9yUG
for (int i = 0; i < alphabetSize; i++) {124Please respect copyright.PENANAFj3prXlLkB
if (count[i][i] % 2 == 0) {124Please respect copyright.PENANAP5XywWKs2x
answer += count[i][i];124Please respect copyright.PENANAcA34YOoB8e
} else {124Please respect copyright.PENANAsSB6oULoZl
answer += count[i][i] - 1;124Please respect copyright.PENANAIac5eV8fh4
central = true;124Please respect copyright.PENANAFfhv0qLuWI
}124Please respect copyright.PENANAK5VIeHz4UT
for (int j = i + 1; j < alphabetSize; j++) {124Please respect copyright.PENANASRQy0KfkLs
answer += 2 * Math.min(count[i][j], count[j][i]);124Please respect copyright.PENANAfwlHujVHUP
}124Please respect copyright.PENANAd5Q9BNkmBK
}124Please respect copyright.PENANAtZZyqfFpcL
if (central) {124Please respect copyright.PENANAF5TpuUhqOd
answer++;124Please respect copyright.PENANAFivKRFR1uO
}124Please respect copyright.PENANAWiJDSauQBh
return 2 * answer;124Please respect copyright.PENANAVkjI2gJglm
}124Please respect copyright.PENANAPGRLbz8q6f
}
124Please respect copyright.PENANAhZFriHd891