Easy Java Solution Longest palindrome By Concatenating Two Letter Words
Longest palindrome By Concatenating Two Letter Words\n\nclass Solution {\n    public int longestPalindrome(String[] words) {\n        //Creating a hashmap to co
**Longest palindrome By Concatenating Two Letter Words**\n```\nclass Solution {\n    public int longestPalindrome(String[] words) {\n        //Creating a hashmap to count frequency of a String\n        HashMap<String,Integer> map=new HashMap<String,Integer>();\n        //Initializing ans variable with maximum possible value\n        int ans=words.length*2;\n        //Initializing two boolean varibales\n        boolean flag=false;  //To check if palindromic substring present in words array\n        boolean flag1=false; //To check if frequency of palindromc substring if present is odd\n        for(int i=0;i<words.length;i++){\n            //If map containsKey then incrementing its frequency by 1\n            if(map.containsKey(words[i])){\n                map.put(words[i],map.get(words[i])+1);\n            }\n            else{\n                //Creating a StringBuilder to reverse the String\n                StringBuilder s=new StringBuilder();\n                s.append(words[i]);\n                s.reverse();\n                String s1=s.toString();\n                //Checking if reverse String present in map if yes then decrementing count of that reverse String by 1 as it could be used for creating palindromic String\n                if(map.containsKey(s1)){\n                    //If count was 1 so decrementing by 1 will make it 0, so removing that String\n                    if(map.get(s1)==1){\n                        map.remove(s1);\n                    }\n                    else{\n                        map.put(s1,map.get(s1)-1);\n                    }\n                }\n                //If map does not contain that reverse String so adding the original String to map\n                else{\n                    map.put(words[i],1);\n                }\n            }\n        }\n        //Now in map we have some palindromic String and other String\n        //Non-palindromic String would not contribute to resultant String\n        for(String word:map.keySet()){\n            //If String is plaindromic then it can be part of resulatnat palindromic String\n            if(word.charAt(0)==word.charAt(1)){\n                flag=true;\n                //If frequency of palindromic String is odd making flag1 true as only even frequency would be used now and as there could many palindromic String of odd frequency but only one odd frequency String would be used rest all even frequency would be used\n                if(map.get(word)%2!=0){\n                    flag1=true;\n                    ans-=2;\n                }\n            }\n            //Else decrementing frequency of that word multiply by 2 from ans\n            //Multiply by 2 as every String contain two letters\n            else{\n                ans-=map.get(word)*2;\n            }\n        }\n        if(flag){\n            //If flag is true so addiing 2 to the ans\n            return flag1==true?ans+2:ans;\n        }\n        return ans;\n    }\n}\n```
['String', 'Greedy']