🏆EASY SOLUTION || Simple Approach || C++ || PYTHON || JAVA🔥🚀💯
\n\n\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n1.Count Character Frequencie
\n\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.Count Character Frequencies:\n\n    ->Create a frequency map (HashMap or unordered_map) to count character frequencies.\n    ->Iterate through each character in the string.\n    ->Increment the count of each character in the frequency map.\n   \n2.Calculate Palindrome Length:\n\n    ->Initialize a variable to store the length of the longest palindrome.\n    ->Iterate through each character frequency in the map.\n    ->For characters with even frequency, add the entire frequency to the length.\n    ->For characters with odd frequency, subtract 1 from the frequency and add it to the length. Set a flag if an odd frequency character is found.\n\n3.Adjust Length for Center Character:\n\n    ->If an odd frequency character is found, increment the length by 1 to account for placing one odd character in the center of the palindrome.\n\n4.Return the Length of the Longest Palindrome:\n\n    Return the final length as the result.\n```C++ []\nclass Solution {\npublic:\n    int longestPalindrome(string s) {\n        int c = 0; // odd count\n        unordered_map<char, int>m;\n        for(auto ch : s) {\n            m[ch]++;\n            if (m[ch] & 1)\n                c++;\n            else    \n                c--;\n        }\n        if (c > 1)\n            return s.length() - c + 1;\n        return s.length();\n    }\n};\n```\n```Python []\n def longestPalindrome(s):\n    c = 0  # odd count\n    freq = {}\n    \n    for ch in s:\n        freq[ch] = freq.get(ch, 0) + 1\n        if freq[ch] % 2 == 0:\n            c -= 1\n        else:\n            c += 1\n    \n    if c > 1:\n        return len(s) - c + 1\n    return len(s)\n```\n```Java []\npublic class Main {\n    public static int longestPalindrome(String s) {\n        int oddCount = 0; // Count of characters with odd frequency\n        HashMap<Character, Integer> frequencyMap = new HashMap<>();\n        \n        for (char ch : s.toCharArray()) {\n            frequencyMap.put(ch, frequencyMap.getOrDefault(ch, 0) + 1);\n            if (frequencyMap.get(ch) % 2 == 0)\n                oddCount--;\n            else\n                oddCount++;\n        }\n        \n        if (oddCount > 1)\n            return s.length() - oddCount + 1;\n        return s.length();\n    }\n```\n\n\n![cat-requesting-upvotes-v0-nkwb9r1n7gyc1.webp](https://assets.leetcode.com/users/images/938d74f7-e960-4a97-ad88-4368ac8a7e62_1717466751.120636.webp)\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(1)\n\n# Code\n```\n\n```
['Hash Table', 'String', 'Greedy', 'C++', 'Java', 'Python3']