JAVA | longest-palindrome-by-concatenating-two-letter-words
\nclass Solution {\n    public int longestPalindrome(String[] words) {\n      int result=0;\n        Map<String,Integer> map=new HashMap<>();\n        for(Strin
```\nclass Solution {\n    public int longestPalindrome(String[] words) {\n      int result=0;\n        Map<String,Integer> map=new HashMap<>();\n        for(String s:words)\n        {\n            map.put(s,map.getOrDefault(s,0)+1);\n        }\n          int c=0;\n        for(String s:map.keySet())\n        {\n            String s2=reverse(s);\n            if(s.equals(s2)){  \n                int val=map.get(s);\n                     if(val%2!=0){val-=1;c=1;}\n                \n                result+=2*val;\n                map.put(s,map.get(s)-val);\n                    \n            }\n            else if(map.containsKey(s2)){\n                int occ=Math.min(map.get(s),map.get(s2));\n                result+=(4*occ);\n              \n                map.put(s,map.get(s)-occ);\n                map.put(s2,map.get(s2)-occ);\n            } \n        }\n        \n        \n        return c==1?result+2:result;\n        \n        \n        \n    }\n    public static String reverse(String str){\n        StringBuilder sb=new StringBuilder(str);\n        return sb.reverse().toString();\n    }\n}\n```
['Java']