2 Easy Interview Approaches ✅ || using HashMap🔥 || without Hasmap💯 || All Language Solution💯
\n\n# Approach - 1\n Describe your approach to solving the problem. \nUsing map - to store the frequency of the characters\n\n# Complexity\n- Time complexity: O
![run time 4 june.png](https://assets.leetcode.com/users/images/3e397920-792a-4a3b-9d9f-73e1650969d5_1717463151.2049053.png)\n\n# Approach - 1\n<!-- Describe your approach to solving the problem. -->\nUsing map - to store the frequency of the characters\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    Map store atmost 52 unique char \n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int longestPalindrome(string s) {\n        map<char,int>m;\n        int cnt=0,one=0;\n        for(int i=0; i<s.size(); i++)\n        {\n            m[s[i]]++;    //Stroe the frequency of characters\n        }\n        for(auto i:m)\n        {\n            if(i.second>1)\n            cnt+=i.second/2*(2);  //Adding even frequency\n\n            if(i.second%2)  //Adding odd frequency if exist\n            one=1;\n        }\n        return cnt+one;\n    }\n};\n```\n```java []\npublic class Solution {\n    public int longestPalindrome(String s) {\n        Map<Character, Integer> m = new HashMap<>();\n        int cnt = 0;\n        int one = 0;\n\n        // Store the frequency of characters\n        for (int i = 0; i < s.length(); i++) {\n            char c = s.charAt(i);\n            m.put(c, m.getOrDefault(c, 0) + 1);\n        }\n\n        for (Map.Entry<Character, Integer> entry : m.entrySet()) {\n            int freq = entry.getValue();\n            if (freq > 1)                 // Adding even frequency\n                cnt += (freq / 2) * 2;\n            \n            if (freq % 2 == 1)                 // Adding odd frequency if it exists\n                one = 1;    \n        }\n        \n        return cnt + one;\n    }\n}\n```\n```Python []\nclass Solution:\n    def longestPalindrome(self, s):\n        m = {}\n        cnt = 0\n        one = 0\n\n        # Store the frequency of characters\n        for ch in s:\n            if ch in m:\n                m[ch] += 1\n            else:\n                m[ch] = 1\n\n        for freq in m.values():\n            if freq > 1:\n                cnt += (freq // 2) * 2    # Adding even frequency\n\n            if freq % 2 == 1:       # Adding odd frequency if it exists\n                one = 1\n\n        return cnt + one\n\n```\n```Python3 []\nclass Solution:\n    def longestPalindrome(self, s: str) -> int:\n        from collections import Counter\n\n        m = Counter(s)\n        cnt = 0\n        one = 0\n\n        for freq in m.values():\n            if freq > 1:\n                # Adding even frequency\n                cnt += (freq // 2) * 2\n\n            # Adding odd frequency if it exists\n            if freq % 2 == 1:\n                one = 1\n\n        return cnt + one\n\n```\n\n\n# Approach - 2\n<!-- Describe your approach to solving the problem. -->\nUsing BitMasking\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    Map store atmost 52 unique char \n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```Python []\nimport collections\nclass Solution:\n    def longestPalindrome(self, s):\n        bits = 0\n\n        result = 0\n        for ch in s:\n            bit = 1 << (ord(ch) - 65)\n            if bits & bit:\n                bits ^= bit\n                result += 2\n            else:\n                bits |= bit\n\n        if bits:\n            result += 1\n\n        return result\n\n```\n\n```C++ []\nclass Solution {\npublic:\n    int longestPalindrome(const std::string &s) {\n        std::vector<int> count(52, 0);  // 26 for lowercase + 26 for uppercase\n        int result = 0;\n\n        for (char ch : s) {\n            if (ch >= \'a\' && ch <= \'z\') {\n                count[ch - \'a\']++;\n            } else if (ch >= \'A\' && ch <= \'Z\') {\n                count[ch - \'A\' + 26]++;\n            }\n        }\n\n        bool oddFound = false;\n        for (int cnt : count) {\n            if (cnt % 2 == 0) {\n                result += cnt;\n            } else {\n                result += cnt - 1;\n                oddFound = true;\n            }\n        }\n        if (oddFound) {\n            result += 1;\n        }\n\n        return result;\n    }\n};\n```\n```java []\npublic class Solution {\n    public int longestPalindrome(String s) {\n        long bits = 0;  // Use 64-bit long to handle both uppercase and lowercase letters\n        int result = 0;\n\n        for (char ch : s.toCharArray()) {\n            long bit;\n            if (ch >= \'a\' && ch <= \'z\') {\n                bit = 1L << (ch - \'a\');  // Lowercase letters\n            } else if (ch >= \'A\' && ch <= \'Z\') {\n                bit = 1L << (ch - \'A\' + 26);  // Uppercase letters, offset by 26\n            } else {\n                continue;  // Ignore other characters\n            }\n\n            if ((bits & bit) != 0) {\n                bits ^= bit;\n                result += 2;\n            } else {\n                bits |= bit;\n            }\n        }\n\n        if (bits != 0) {\n            result += 1;\n        }\n\n        return result;\n    }\n}\n\n```\n![upvote cat3.png](https://assets.leetcode.com/users/images/58aa27ee-607d-49f7-bbcc-7d6aa9265774_1717463177.6964986.png)\n\n
['Hash Table', 'String', 'Ordered Map', 'Swift', 'Python', 'C++', 'Java']