✅✅|| Easiest Solution || Approach || C++ || JAVA || PYTHON || JAVASCRIPT || Beginner Friendly🔥🔥🔥
Intuition\n Describe your first thoughts on how to solve this problem. \nTo form the longest palindrome from the given string, we need to understand the propert
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo form the longest palindrome from the given string, we need to understand the properties of a palindrome:\n\nA palindrome reads the same forwards and backwards.\nFor this to happen, each character must appear an even number of times so they can be mirrored around the center.\nAt most one character can appear an odd number of times (it will be placed in the center).\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nCount the frequency of each character in the string.\nInitialize a variable to keep track of the length of the longest palindrome.\nIterate over the frequency counts:\nIf a character count is even, it can be fully used in the palindrome.\nIf a character count is odd, use the largest even part of it (e.g., if the count is 5, use 4 of them), and keep track of the fact that you have an odd character available.\nIf there is at least one odd character, one of these can be placed in the center of the palindrome.\nThe result will be the sum of the lengths computed from the above steps.\n\n# Explaination\nCounting Frequencies and Odd Counts:\n\nAs you iterate through each character in the string, you count the occurrences of each character using an unordered map.\nFor each character, you update the oddCount based on whether the current count of that character is odd or even.\nIf a character\'s count becomes odd, you increment oddCount.\nIf a character\'s count becomes even, you decrement oddCount.\n\n\nDetermining the Longest Palindrome Length:\n\nIf there is more than one character with an odd count, the longest palindrome can use all but one of those odd-count characters (which will be placed in the center). Thus, s.length() - oddCount + 1 gives the correct length.\nIf there is at most one character with an odd count, the entire string can be rearranged into a palindrome.\n\n# Complexity\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# Code\n```C++ []\nclass Solution {\npublic:\n    int longestPalindrome(string s) {\n        int oddFrequencyCount = 0;\n        unordered_map<char, int> charFrequency;\n        for (char ch : s) {\n            charFrequency[ch]++;\n            if (charFrequency[ch] % 2 == 1)\n                oddFrequencyCount++;\n            else\n                oddFrequencyCount--;\n        }\n        if (oddFrequencyCount > 1)\n            return s.length() - oddFrequencyCount + 1;\n        return s.length();\n    }\n};\n```\n```Java []\npublic class Solution {\n    public int longestPalindrome(String s) {\n        HashMap<Character, Integer> charFrequency = new HashMap<>();\n        int oddFrequencyCount = 0;\n        for (char ch : s.toCharArray()) {\n            charFrequency.put(ch, charFrequency.getOrDefault(ch, 0) + 1);\n            if (charFrequency.get(ch) % 2 == 1)\n                oddFrequencyCount++;\n            else\n                oddFrequencyCount--;\n        }\n        if (oddFrequencyCount > 1)\n            return s.length() - oddFrequencyCount + 1;\n        return s.length();\n    }\n}\n```\n```python []\nclass Solution:\n    def longestPalindrome(self, s: str) -> int:\n        charFrequency = Counter(s)\n        oddFrequencyCount = 0\n        for frequency in charFrequency.values():\n            if frequency % 2 == 1:\n                oddFrequencyCount += 1\n        if oddFrequencyCount > 1:\n            return len(s) - oddFrequencyCount + 1\n        return len(s)\n```\n```Javascript []\nclass Solution {\n    longestPalindrome(s) {\n        let charFrequency = {};\n        let oddFrequencyCount = 0;\n        for (let char of s) {\n            charFrequency[char] = (charFrequency[char] || 0) + 1;\n            if (charFrequency[char] % 2 === 1)\n                oddFrequencyCount++;\n            else\n                oddFrequencyCount--;\n        }\n        if (oddFrequencyCount > 1)\n            return s.length - oddFrequencyCount + 1;\n        return s.length;\n    }\n}\n```\n\n![upvote.png](https://assets.leetcode.com/users/images/0e38bb6f-630d-40fd-b054-20d45b0f650d_1717459756.9780626.png)\n\n
['String', 'Python', 'C++', 'Java', 'JavaScript']