
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 {210Please respect copyright.PENANAZB0lxb1U1X
public int longestPalindrome(String[] words) {210Please respect copyright.PENANA1cLNgspaDy
HashMap<String, Integer> count = new HashMap<String, Integer>();210Please respect copyright.PENANAZbPRQpqd1U
// Count the number of occurrences of each word using a hashmap210Please respect copyright.PENANAfsJXm56KKl
for (String word : words) {210Please respect copyright.PENANA3nozC5bOWJ
Integer countOfTheWord = count.get(word);210Please respect copyright.PENANAuSd8xHF4bG
if (countOfTheWord == null) {210Please respect copyright.PENANAGZpLuu2nCE
count.put(word, 1);210Please respect copyright.PENANAyTE9nbNVLg
} else {210Please respect copyright.PENANABhmDW13JbT
count.put(word, countOfTheWord + 1);210Please respect copyright.PENANAc5b7F4kQMZ
}210Please respect copyright.PENANAYmEhCw6PNC
}210Please respect copyright.PENANAiVyNMzu51q
// 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 word210Please respect copyright.PENANAMR7h9FE4wD
int answer = 0;210Please respect copyright.PENANATvBprhoEED
boolean central = false;210Please respect copyright.PENANATosWG61KAj
for (Map.Entry<String, Integer> entry : count.entrySet()) {210Please respect copyright.PENANAnCS1Ewfcju
String word = entry.getKey();210Please respect copyright.PENANANJoqgAz1xk
int countOfTheWord = entry.getValue();210Please respect copyright.PENANARzG5rwY5LK
// if the word is a palindrome210Please respect copyright.PENANAocpaERMfrJ
if (word.charAt(0) == word.charAt(1)) {210Please respect copyright.PENANAsH66saHaAy
if (countOfTheWord % 2 == 0) {210Please respect copyright.PENANAw7JpmwoFQb
answer += countOfTheWord;210Please respect copyright.PENANA7JJRzmc4hf
} else {210Please respect copyright.PENANAKtMEqSdcKL
answer += countOfTheWord - 1;210Please respect copyright.PENANAZ6EHzULs8A
central = true;210Please respect copyright.PENANAD5MuCJ9XEN
}210Please respect copyright.PENANA9M58BwJODj
// consider a pair of non-palindrome words such that one is the reverse of another210Please respect copyright.PENANA5NjOO9v2qL
} else if (word.charAt(0) < word.charAt(1)) {210Please respect copyright.PENANAFXXLzggC9v
String reversedWord = "" + word.charAt(1) + word.charAt(0);210Please respect copyright.PENANAEKRbsKGVMj
if (count.containsKey(reversedWord)) {210Please respect copyright.PENANAy7OeVdRdEb
answer += 2 * Math.min(countOfTheWord, count.get(reversedWord));210Please respect copyright.PENANARIh1V7eoy7
}210Please respect copyright.PENANA00XgaBscaZ
}210Please respect copyright.PENANAMr3rCdnb25
}210Please respect copyright.PENANA6PJqCLdsHk
if (central) {210Please respect copyright.PENANAcK7NlxKRHq
answer++;210Please respect copyright.PENANAyq0GxZrgcE
}210Please respect copyright.PENANAcRFzrkfWuC
return 2 * answer;210Please respect copyright.PENANAwKgPlKoXWg
}210Please respect copyright.PENANA8yH8TQVqh5
}
2: A Two-Dimensional Array Approach
class Solution {210Please respect copyright.PENANAdKoc0FFyfT
public int longestPalindrome(String[] words) {210Please respect copyright.PENANARLwIvSnKZT
final int alphabetSize = 26;210Please respect copyright.PENANAtvbZvEWulw
int[][] count = new int[alphabetSize][alphabetSize];210Please respect copyright.PENANAUjk6BdJifK
// Count the number of occurrences of each word using a two-dimensional array. 210Please respect copyright.PENANAY4jkFpdW70
for (String word : words) {210Please respect copyright.PENANAOQ2gwBI4bb
count[word.charAt(0) - 'a'][word.charAt(1) - 'a']++;210Please respect copyright.PENANAI5PCkAcdpD
}210Please respect copyright.PENANAoMrQvd6igP
// The answer will denote the number of words in the final string and the boolean variable central will denote whether we have a central word210Please respect copyright.PENANA17QEcCqSaO
int answer = 0;210Please respect copyright.PENANAn8ljG4Du72
boolean central = false;210Please respect copyright.PENANAvmvO7FDjEq
for (int i = 0; i < alphabetSize; i++) {210Please respect copyright.PENANATpj22LLbcG
if (count[i][i] % 2 == 0) {210Please respect copyright.PENANAHOcccS3taR
answer += count[i][i];210Please respect copyright.PENANAddVeNkLxHS
} else {210Please respect copyright.PENANAb2qMZLx5kH
answer += count[i][i] - 1;210Please respect copyright.PENANAhjKNZP8sgm
central = true;210Please respect copyright.PENANAfwOdACn4f2
}210Please respect copyright.PENANAiuRpLXhQy8
for (int j = i + 1; j < alphabetSize; j++) {210Please respect copyright.PENANAS7izN54Ihn
answer += 2 * Math.min(count[i][j], count[j][i]);210Please respect copyright.PENANA4MvwWEnzyu
}210Please respect copyright.PENANA5BeP5rgBjL
}210Please respect copyright.PENANAYsZheZvm6O
if (central) {210Please respect copyright.PENANA91wixKviWl
answer++;210Please respect copyright.PENANArw7IaJzrlA
}210Please respect copyright.PENANAyQhhn3sVnF
return 2 * answer;210Please respect copyright.PENANAfKTm7yZMSG
}210Please respect copyright.PENANA4Xjy6yylmo
}
210Please respect copyright.PENANAfHyzyDAeoJ