Fast and Easy solution by using frequency of characters concept | HashMap |C++ and Java solution 💯✅
Intuition \uD83E\uDDE0\n Describe your first thoughts on how to solve this problem. \nWe need to have store frequency of each character to check whether it is o
# Intuition \uD83E\uDDE0\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe need to have store frequency of each character to check whether it is odd or even.\n\n---\n\n\n# Approach \uD83E\uDE9C\n<!-- Describe your approach to solving the problem. -->\n1. Make a map which will store the frequency of each character of string s.\n2. Initialize variables for counting longest length of palindrome`(len = 0)` and second for checking if odd freq has encountered`(flag = false)`.\n3. Now put characters of string s in hm along with its freq.\nSo `key of hm is char of s` and `value of hm is its freq`.\n\n```java []\nHashMap<Character, Integer> hm = new HashMap<>();\nint len = 0;\nboolean flag = false;\n\nfor (char ch : s.toCharArray()) {\n    hm.put(ch, hm.getOrDefault(ch, 0) + 1);\n}\n```\n```C++ []\nmap<char, int> hm;\nint len = 0;\nbool flag = false;\n\nfor (char ch : s) {\n    hm[ch]++;\n}\n```\n4. Now check the values of hm is odd or not.\n5. If freq is `even` then simply add its `freq` in len.\n6. But if the freq is `odd` then add `(freq - 1)` in its len and \nmake `flag = true`.\nFor example String s = "cccddd"\nhm will store` key = \'c\', val = 3 & key = \'d\', val = 3`\nBoth the frequencies are odd therefore len in the loop would be equal to `4`. But longest palidrome could be of length `5` therefore we will return `len + 1 when flag = true`. \n\n```java []\nfor (int freq : hm.values()) {\n    if (freq % 2 == 0) { // even\n        len += freq;\n    } else { //odd \n        len += freq - 1;\n        flag = true;\n    }\n}\n\nreturn flag == true ? len + 1 : len;\n```\n```C++ []\nfor (auto& el : hm) {\n    int freq = el.second;\n    if (freq % 2 == 0) {\n        len += freq;\n    } else {\n        len += freq - 1;\n        flag = true;\n    }\n}\n\nreturn flag == true ? len + 1 : len;\n```\n\n7. We will not simply add 1 to len if freq is odd since it will fail cases \nfor `freq > 1`. \nTherefore we are adding first (freq - 1) then at last adding +1 if odd has been encountered otherwise we will return len.\n\n---\n\n\n# Complexity \uD83D\uDC69\u200D\uD83D\uDCBB\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n---\n\n\n\n# Code \uD83D\uDE0A\uD83D\uDCBB\n```java []\nclass Solution {\n    public int longestPalindrome(String s) {\n        HashMap<Character, Integer> hm = new HashMap<>();\n        int len = 0;\n        boolean flag = false;\n\n        for (char ch : s.toCharArray()) {\n            hm.put(ch, hm.getOrDefault(ch, 0) + 1);\n        }\n\n        for (int freq : hm.values()) {\n            if (freq % 2 == 0) { \n                len += freq;\n            } else {\n                len += freq - 1;\n                flag = true;\n            }\n        }\n\n        return flag == true ? len + 1 : len;\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    int longestPalindrome(string s) {\n        map<char, int> hm;\n        int len = 0;\n        bool flag = false;\n\n        for (char ch : s) {\n            hm[ch]++;\n        }\n\n        for (auto& el : hm) {\n            int freq = el.second;\n            if (freq % 2 == 0) {\n                len += freq;\n            } else {\n                len += freq - 1;\n                flag = true;\n            }\n        }\n\n        return flag == true ? len + 1 : len;\n    }\n};\n```\n\n---\n\n*Upvote\uD83D\uDC4D and comment\u2328\uFE0F.\nPlease let me know if it helped you.*\n\n**THANK YOU! \uD83D\uDE0A**
['Hash Table', 'String', 'C++', 'Java']