Optimize Hashmap Solution
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\nO(n)+(hashmap.size())\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution {\n    public int longestPalindrome(String[] words) {\n        HashMap<String,Integer> hm = new HashMap<>();\n        int ans=0,flag=0;\n        for(int i=0;i<words.length;i++){\n            hm.put(words[i],hm.getOrDefault(words[i],0)+1);\n        }\n        for(String word : hm.keySet()){\n            char ch= word.charAt(0);\n            char ch1= word.charAt(1);\n            String rev=""+ch1+ch;\n            if(hm.containsKey(rev)){\n                if(ch == ch1){\n                    if(hm.get(word)%2==0){\n                        ans+=hm.get(word)*2;\n                    }\n                    else{\n                        if(flag==0){\n                            ans+=hm.get(word)*2;\n                            flag=1;\n                        }\n                        else{\n                            ans+=(hm.get(word)-1)*2;\n                        }\n                    }\n                }\n                if(ch != ch1) ans+=Math.min(hm.get(word),hm.get(rev))*2;\n\n            }\n\n\n        }\n        return ans;\n    }\n}\n```
['Hash Table', 'Java']