💯✅🔥Easy Java ,Python3 ,C++ Solution|| 3 ms ||≧◠‿◠≦✌
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem is to find the length of the longest palindromic substring in a given strin
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is to find the length of the longest palindromic substring in a given string. A palindromic substring is a substring that reads the same backward as forward. For example, "aba" is a palindromic substring of "ababa".\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe approach used in the code is to count the frequency of each character in the string. Then, for each character, if its frequency is odd, it can be part of the longest palindromic substring only once. So, we subtract 1 from its frequency and add it to the total length of the longest palindromic substring. If the frequency is even, the character can be part of the longest palindromic substring as many times as its frequency. We add its frequency to the total length.\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n\n```Java []\nclass Solution {\n    public int longestPalindrome(String s) {\n        int res = 0, val = 0;\n        Map<Character, Integer> mp = new HashMap<>();\n        for (char ch : s.toCharArray()) {\n            mp.put(ch, mp.getOrDefault(ch, 0) + 1);\n        }\n        for (Map.Entry<Character, Integer> entry : mp.entrySet()) {\n            if (entry.getValue() % 2 != 0) {\n                res += entry.getValue() - 1;\n                val = 1;\n            } else {\n                res += entry.getValue();\n            }\n        }\n        return res + val;\n    }\n}\n```\n```python3 []\nclass Solution:\n    def longestPalindrome(self, s: str) -> int:\n        res = 0\n        val = 0\n        mp = {}\n        for ch in s:\n            mp[ch] = mp.get(ch, 0) + 1\n        for count in mp.values():\n            if count % 2 != 0:\n                res += count - 1\n                val = 1\n            else:\n                res += count\n        return res + val\n```\n```C++ []\nclass Solution {\npublic:\n    int longestPalindrome(string s) {\n        int res = 0, val = 0;\n        unordered_map<char, int> mp;\n        for (char ch : s) {\n            mp[ch]++;\n        }\n        for (auto entry : mp) {\n            if (entry.second % 2 != 0) {\n                res += entry.second - 1;\n                val = 1;\n            } else {\n                res += entry.second;\n            }\n        }\n        return res + val;\n    }\n};\n```\n![upvote.png](https://assets.leetcode.com/users/images/5ea63c40-e902-4c4b-8b1c-0141a76b1209_1717460894.1913474.png)\n\n
['String', 'Greedy', 'C++', 'Java', 'Python3']