Good to Go ✅🚀
Dry run it you will get it no further explination can make you understand it.\n\nThe best Solution you will ever find.\n\nCredits: tojuna\n\n# Code\njava []\ncl
Dry run it you will get it no further explination can make you understand it.\n\nThe best Solution you will ever find.\n\nCredits: [tojuna](https://leetcode.com/problems/longest-palindrome-by-concatenating-two-letter-words/solutions/1675343/python3-java-c-counting-mirror-words-o-n/)\n\n# Code\n```java []\nclass Solution {\n    public int longestPalindrome(String[] words) {\n        // Create a map to count the frequency of each word\n        Map<String, Integer> wordCount = new HashMap<>();\n        \n        // Populate the map with word frequencies\n        for (String word : words) {\n            wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);\n        }\n        \n        // Variables to keep track of the length of the palindrome and if we can use a central word\n        int center = 0;\n        int length = 0;\n        \n        // Iterate over each word to calculate possible palindrome length\n        for (String word : words) {\n            // Reverse the current word\n            String reversedWord = new StringBuilder(word).reverse().toString();\n            \n            // Check if the current word is a palindrome (same as its reverse)\n            if (word.equals(reversedWord)) {\n                // If the word is a palindrome, use pairs of these words\n                int count = wordCount.get(word);\n                length += 4 * (count / 2); // Each pair contributes 4 to the length\n                if (count % 2 == 1) {\n                    // If there\'s an odd number of palindromic words, one can be used in the center\n                    center = 1;\n                }\n                // Set the count to zero to avoid reprocessing this word\n                wordCount.put(word, 0);\n            } else if (wordCount.containsKey(reversedWord)) {\n                // If the word is not a palindrome but its reverse is present\n                int countWord = wordCount.get(word);\n                int countReversedWord = wordCount.get(reversedWord);\n                // Use pairs of the current word and its reversed counterpart\n                length += 4 * Math.min(countWord, countReversedWord);\n                // Set counts to zero to avoid reprocessing these words\n                wordCount.put(word, 0);\n                wordCount.put(reversedWord, 0);\n            }\n        }\n        \n        // Add the central palindrome word if we found any\n        return center * 2 + length;\n    }\n}\n\n```\n```java []\npublic class Solution {\n    public int longestPalindrome(String[] words) {\n        // Initialize a 2D array to keep track of pairs of characters\n        int[][] wordPairsCount = new int[26][26];\n        int palindromeLength = 0;\n\n        // Iterate through each word in the input array\n        for (String word : words) {\n            // Calculate the indices of the first and second characters\n            int firstCharIndex = word.charAt(0) - \'a\';\n            int secondCharIndex = word.charAt(1) - \'a\';\n\n            // Check if the reverse pair exists\n            if (wordPairsCount[secondCharIndex][firstCharIndex] > 0) {\n                // If reverse pair exists, add 4 to the palindrome length\n                palindromeLength += 4;\n                // Decrement the count of the reverse pair\n                wordPairsCount[secondCharIndex][firstCharIndex]--;\n            } else {\n                // If reverse pair does not exist, increment the count of the current pair\n                wordPairsCount[firstCharIndex][secondCharIndex]++;\n            }\n        }\n\n        // Check for any remaining same-letter words\n        for (int i = 0; i < 26; i++) {\n            if (wordPairsCount[i][i] > 0) {\n                // If such a word exists, add 2 to the palindrome length\n                palindromeLength += 2;\n                break;\n            }\n        }\n\n        // Return the length of the longest palindrome\n        return palindromeLength;\n    }\n}\n\n```
['Java']