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 {74Please respect copyright.PENANAjkh2AVPaFl
public int longestPalindrome(String[] words) {74Please respect copyright.PENANAsVuqOB5Mhh
HashMap<String, Integer> count = new HashMap<String, Integer>();74Please respect copyright.PENANAGUyzCdi5X6
// Count the number of occurrences of each word using a hashmap74Please respect copyright.PENANAljU6z0tWy0
for (String word : words) {74Please respect copyright.PENANAuBIqRLVdPM
Integer countOfTheWord = count.get(word);74Please respect copyright.PENANADB9SnUZDog
if (countOfTheWord == null) {74Please respect copyright.PENANARclK9ozJ4c
count.put(word, 1);74Please respect copyright.PENANALKVXNM4LZE
} else {74Please respect copyright.PENANA17f9tfyQKo
count.put(word, countOfTheWord + 1);74Please respect copyright.PENANAAcCYCIA5dP
}74Please respect copyright.PENANAPB9VBQNnB7
}74Please respect copyright.PENANAI5ljI9kCOG
// 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 word74Please respect copyright.PENANASPSp7CBROh
int answer = 0;74Please respect copyright.PENANAvp7CtZT0uJ
boolean central = false;74Please respect copyright.PENANAp5jnRtUYmm
for (Map.Entry<String, Integer> entry : count.entrySet()) {74Please respect copyright.PENANAw9VOO54Zz8
String word = entry.getKey();74Please respect copyright.PENANAC8Ah7m1Y1a
int countOfTheWord = entry.getValue();74Please respect copyright.PENANAWw25TWCLkl
// if the word is a palindrome74Please respect copyright.PENANAnYlM1CeA3o
if (word.charAt(0) == word.charAt(1)) {74Please respect copyright.PENANAnsb9xc9oYB
if (countOfTheWord % 2 == 0) {74Please respect copyright.PENANAtozjDmGScF
answer += countOfTheWord;74Please respect copyright.PENANALIqu88DTti
} else {74Please respect copyright.PENANAPUxnT2l5Rz
answer += countOfTheWord - 1;74Please respect copyright.PENANAMgdxl6l2Um
central = true;74Please respect copyright.PENANAcBbzlYSpqa
}74Please respect copyright.PENANA2jTkntMpwK
// consider a pair of non-palindrome words such that one is the reverse of another74Please respect copyright.PENANAS5d6zm1hHM
} else if (word.charAt(0) < word.charAt(1)) {74Please respect copyright.PENANA7ditpgbTXy
String reversedWord = "" + word.charAt(1) + word.charAt(0);74Please respect copyright.PENANAWC9LSAz3O3
if (count.containsKey(reversedWord)) {74Please respect copyright.PENANAXLJG6HKHcD
answer += 2 * Math.min(countOfTheWord, count.get(reversedWord));74Please respect copyright.PENANALAAC86YTDd
}74Please respect copyright.PENANAcZ9Cq5THY5
}74Please respect copyright.PENANAXi0TY23wrq
}74Please respect copyright.PENANA7i5uQSkGw6
if (central) {74Please respect copyright.PENANAPJ3Tc42700
answer++;74Please respect copyright.PENANAMXoGlLzArp
}74Please respect copyright.PENANAhLZKdS8Px5
return 2 * answer;74Please respect copyright.PENANA0cLT2CiBY3
}74Please respect copyright.PENANAFUG7Pro66U
}
2: A Two-Dimensional Array Approach
class Solution {74Please respect copyright.PENANAHML5X2M2X9
public int longestPalindrome(String[] words) {74Please respect copyright.PENANA0jDUVtVOM5
final int alphabetSize = 26;74Please respect copyright.PENANAOTdAIo7vJL
int[][] count = new int[alphabetSize][alphabetSize];74Please respect copyright.PENANAHJLAA4iKcL
// Count the number of occurrences of each word using a two-dimensional array. 74Please respect copyright.PENANAuL7EUnDYDm
for (String word : words) {74Please respect copyright.PENANAR3nFWpLkWp
count[word.charAt(0) - 'a'][word.charAt(1) - 'a']++;74Please respect copyright.PENANAy6vsTgwEWU
}74Please respect copyright.PENANAywdIWtq4wO
// The answer will denote the number of words in the final string and the boolean variable central will denote whether we have a central word74Please respect copyright.PENANAmNAKlpDAW4
int answer = 0;74Please respect copyright.PENANAJhg2lEknkk
boolean central = false;74Please respect copyright.PENANAtFqXGZHRUJ
for (int i = 0; i < alphabetSize; i++) {74Please respect copyright.PENANAi99XU9J7kl
if (count[i][i] % 2 == 0) {74Please respect copyright.PENANAU7fRNQk8p9
answer += count[i][i];74Please respect copyright.PENANAeABAXl43Xq
} else {74Please respect copyright.PENANAlsqIBCCKVC
answer += count[i][i] - 1;74Please respect copyright.PENANAITcJuVTqgC
central = true;74Please respect copyright.PENANA6GV0X2u53l
}74Please respect copyright.PENANAjOv0kN4THR
for (int j = i + 1; j < alphabetSize; j++) {74Please respect copyright.PENANAjGlw9ll3cf
answer += 2 * Math.min(count[i][j], count[j][i]);74Please respect copyright.PENANAAG2u0OSU29
}74Please respect copyright.PENANAKMrqt5oQ8N
}74Please respect copyright.PENANAodThI1iysZ
if (central) {74Please respect copyright.PENANAb9aRJB2o8Y
answer++;74Please respect copyright.PENANAEn46TCLIsZ
}74Please respect copyright.PENANAfybayITZ71
return 2 * answer;74Please respect copyright.PENANA627728b9uA
}74Please respect copyright.PENANAurJVuChWSs
}
74Please respect copyright.PENANAYyEyEcQtep