Java || HashMap || Simple Observation
if words contains reverse of the same words ,then it must be a part of the answer.\n2.   for already palindrome words like "gg" or " rr" ,count++;\n3.   if alre
1.   if words contains reverse of the same words ,then it must be a part of the answer.\n2.   for already palindrome words like "gg" or " rr" ,count++;\n3.   if already palindrome words have also the pair ,both are part of the answer and count--;\n4.   if(count>0) then add 2 to your answer\n\n\ncode ......\n\nclass Solution {\n    public int longestPalindrome(String[] words) {\n        \n        HashMap<String,Integer> map=new HashMap<>();\n        int ans=0;\n        int count=0;\n        for(int i=0;i<words.length;i++){\n            String s=words[i].charAt(1) +""+ words[i].charAt(0);\n            if(s.equals(words[i])==true){\n                if(map.containsKey(s)==true){\n                    count--;\n                    ans+=4;\n                    map.remove(s);\n                }\n                else{\n                    count++;\n                    map.put(s,1);\n                }\n                \n            }\n            else{\n                \n                int v=map.getOrDefault(s,0);\n                if(v==0){\n                    int k=map.getOrDefault(words[i],0);\n                    k++;\n                    map.put(words[i],k);\n                }\n                else{\n                    v--;\n                    ans+=4;\n                    // System.out.println(i);\n                    if(v==0){\n                        map.remove(s);\n                    }\n                    else{\n                        map.put(s,v);\n                    }\n                }\n            }\n        }\n        // System.out.println(count);\n        return count>0 ? ans+2 : ans;\n    }\n}
['Java']