☕JAVA || 🚀FASTEST || ✨EASY EXPLAINED || O(n) || 2 APPROACHES
\nApproach 1:\n1. We have 26 total alphabets so we can store their pair in 2626 matrix.\n2. If we get both char same then we will add 2(because adding them more
\nApproach 1:\n1. We have 26 total alphabets so we can store their pair in 26*26 matrix.\n2. If we get both char same then we will add 2(because adding them more wil not make the string palindrome)\n3. When we find a pair of string(eg. "ab","ba"), then we add 4 to our answer.\n\nTime: O(n);\nSpace: O(1) (constant space);\n```\npublic int longestPalindrome(String[] words) {\n   int result = 0;//store final answer\n   int freq[][] = new int[26][26];//for keeping frequency of all words\n   \n    for (String s: words) {// iterating over all words\n        int i = s.charAt(0) - \'a\';// first char\n\t\tint j = s.charAt(1) - \'a\';// second char\n        if (freq[j][i] > 0) {// if reverse word have atleast one frequency\n            result += 4; //then we got one pair so add 4\n            freq[j][i]--; // one pair is found so decrement freq by only 1 as we haven\'t added [i][j]th word yet.\n        }else{\n\t\t\tfreq[i][j]++;// if no pair found then increment frequency\n\t\t}\t\n    }\n\t\n    for (int i = 0; i < 26; i++) {\n        if (freq[i][i] > 0) {// [i][i] index bcz every similar word will be at diagonal position\n            result += 2;\n            break; //if more similar words are added then our string will not be a palindrome\n        }\n    }\n    return result;\n}\n```\n\nApproach 2:\n\n**Conditions are similar just steps to count the frequency are different**\n\nTIme:O(n);\nSpace:O(1) (constant size array)\n```\nclass Solution {\n  public int longestPalindrome(String[] words) {\n    int[][] freq = new int[26][26];//array for all alphabet combinations\n    for (String word : words)\n      freq[word.charAt(0) - \'a\'][word.charAt(1) - \'a\']++;// here we first increase the freq for every word\n    int left = 0;//to store freq counts\n    boolean odd = false;\n    for (int i = 0; i != 26; i++) {//iterate over our array\n      odd |= (freq[i][i] & 1) == 1;//means odd number of freq for similar words are there\n      left += freq[i][i] / 2;\n      for (int j = i + 1; j != 26; j++)//nested iteration to find non similar pairs\n        left += Math.min(freq[i][j], freq[j][i]);//taking min times from both present \n    }\n    int res = left * 2 * 2;//res from total freq found!!\n    if (odd){\n        res+=2;// if odd then adding 2\n    } \n    return res;\n  }\n}\n```\nDo like if it helped you :)
['Java']