~~Highly~~ || Extreme Visualise Solution || 2 Approaches 😱😱
We will have a string, using that characters from the string. We have to tell that. How much longest length palindrome is possible. \nAnd we have to return that
We will have a string, using that characters from the string. We have to tell that. How much longest length palindrome is possible. \nAnd we have to return that longest length.\n\nWhat is **Palindrome**?\n> For example we have given a character, `a` if you read it from left to right or right to left. It going to meant the same thing!\n\n![image](https://assets.leetcode.com/users/images/adca8198-2d09-4abe-bfe5-ff74955c5fa0_1717460130.9780352.png)\n\n> What if we have **`ab`**, is it a palindromic string?\n\n![image](https://assets.leetcode.com/users/images/acc6ee94-0abc-4a14-8c89-d6bd34d128ce_1717460223.6292279.png)\n* Because **left to right**, we will read it will be **ab**\n* But from **right to left**, we will read it will be **ba**\n* Hence, it\'s not\n\n> What if we have **`ab`**, is it a palindromic string?\n\n![image](https://assets.leetcode.com/users/images/9c708b8c-1377-4a02-a744-61bfe01bdab7_1717460366.6658869.png)\n* Because **left to right**, we will read it will be **aba**\n* But from **right to left**, we will read it will be **aba**\n* Hence, it\'s is palindromic string\n\n**Approach - 1**\n\nSo, we don\'t have to check whether it is palindrome or not! We have to return that, using that string character, how long palindrome can we make!\nLet\'s take one example,\n```\nInput: s = "abccccdd"\nOutput: 7\n```\nWhat will be the longest palindrome, can we make using this string **s**. We can use it\'s character in any order. And we have to return the longest length. We don\'t have to make it, we just have to return the length!\n\nLet;s have few palindrome first,\n```\naabb\nabba\naccca\naaabaaa\n```\nNow, if you carefully notice that. These are only making palindrome, as if the frequency of a character is in **even number**!\n![image](https://assets.leetcode.com/users/images/82e7d1af-cebc-4b6f-b0b5-3916f6c32420_1717460944.1859243.png)\n\nBut, this isn\'t true, because if you look at other examples you will see that, the frequency of other characters in other strings are **odd too**\n![image](https://assets.leetcode.com/users/images/c92cd3fb-fc1e-47a7-a35c-a2bd2434e7e4_1717461094.39627.png)\n\nSo, what this thing conclude! Can both **even** and **odd** frequency makes a palindrome, ahhhh NO! \nLet\'s understand with one more example!\n![image](https://assets.leetcode.com/users/images/b5e29e65-b1a3-4262-a23d-abaa8170eaba_1717461366.975243.png)\n\nNow in this above, example you can see that\n* `a` has 4 characters, `b` has 1 character and `c` has 3 characters\n* So, the only palindrome can be possible is, on the outer side we got even characters and in the middle or in the inner we have only **1 odd** character present. It\'s frequency doesn\'t matter, but to be a palindrome. It has to be 1 only!\n* See what I\'m saying, This is one possible palindrome\n![image](https://assets.leetcode.com/users/images/bb9b2708-e3ce-4ad1-8458-76843ced2310_1717461532.689692.png)\nAnd this is another one possible palindrome\n![image](https://assets.leetcode.com/users/images/6da1bb42-89ad-4011-b914-adcfc47680e0_1717461588.7535245.png)\n\nNow, from this what we can conclude is :\n* Either give us all **even** frequency characters\n* Or, give us **even** + **odd (frequency character has to be 1 only)**\n\nNow let\'s try to solve this problem, using the given example!\n* First, we going to create it\'s frequency map!\n![image](https://assets.leetcode.com/users/images/88582be6-52be-440b-b820-f17a852fb69d_1717461891.5485165.png)\n\nNow, from this **frequency map**, how many **even** characters and **one odd** character we can take!\n* `c` is **4**\n* `d` is **2**\n* Now from `a`**/**`b` we can choose anyone as there frequency are same!\n* And return the sum of their frequency i.e. **`4 + 2 + 1 => 7`**\n![image](https://assets.leetcode.com/users/images/77c95d59-4e2a-42d4-bc08-fe7152285177_1717462121.8960083.png)\n\n **Let\'s understand with one more example**\n ```\n Input: s = "aabbbbbcccdd"\nOutput: 11\n ```\n Now let\'s try to solve this problem, using the given example!\n* First, we going to create it\'s frequency map!\n* `c` is **3**\n* `d` is **2**\n* `a` is **2**\n* `b` is **5**\n* And return the, maximum possible length of their frequency i.e. **`2 + 2 + 5 => 9`**\n* But this is wrong, it can\'t be possible\n![image](https://assets.leetcode.com/users/images/4f083123-8ca8-4f89-92ff-cc2291f87f00_1717462460.355122.png)\n\nWhy so, it\'s not possible! Because it doesn\'t mean if we have to take `c` character, we have to take it\'s all occurence. We can take only few as well.\nEven, characters we have to take at all cost. But, let\'s say we first take `b` all occuerence, but about the `c` has 3 occuerence, but we have already consider one odd previously i.e. `b`, but let\'s do one thing! Let\'s reduce down the `c` frequency by 1 to **2**. Now `c` is even, and we can have `c` too,. Let\'s look at it\'s diagram\n![image](https://assets.leetcode.com/users/images/904b5720-8d81-49e3-99ea-1b61d15f80aa_1717462818.4382663.png)\n\nThe biggest possible palindrome, we can have is of length **11**\n\nSo, now let\'s code it UP!\n\n**C++**\n```\nclass Solution {\npublic:\n    int longestPalindrome(string s) {\n        unordered_map<char, int> hm;\n        for (int i = 0; i < s.length(); i++) {\n            char c = s[i];\n            if (hm.find(c) != hm.end()) {\n                hm[c]++;\n            } else {\n                hm[c] = 1;\n            }\n        }\n\n        int ans = 0;\n        bool isFirstOdd = false;\n\n        for (auto& pair : hm) {\n            if (pair.second % 2 == 0) {\n                ans += pair.second;\n            } else {\n                ans += pair.second;\n                if (!isFirstOdd) {\n                    isFirstOdd = true;\n                } else {\n                    ans -= 1;\n                }\n            }\n        }\n\n        return ans;\n    }\n};\n```\n**JAVA**\n```\nclass Solution {\n    public int longestPalindrome(String s) {\n        HashMap<Character, Integer> hm = new HashMap<>();\n    for (int i = 0; i < s.length(); i++) {\n        char c = s.charAt(i);\n        if (hm.containsKey(c)) {\n            hm.put(c, hm.get(c) + 1);\n        } else {\n            hm.put(c, 1);\n        }\n    }\n\n    int ans = 0;\n    boolean isFirstOdd = false;\n\n    for (Character key : hm.keySet()) {\n        if (hm.get(key) % 2 == 0) {\n            ans += hm.get(key);\n        } else {\n            ans += hm.get(key);\n            if (!isFirstOdd) {\n                isFirstOdd = true;\n            } else {\n                ans -= 1;\n            }\n        }\n    }\n\n    return ans;\n    }\n}\n```\n**PYTHON**\n```\nclass Solution:\n    def longestPalindrome(self, s: str) -> int:\n        hm = {}\n        for c in s:\n            if c in hm:\n                hm[c] += 1\n            else:\n                hm[c] = 1\n\n        ans = 0\n        isFirstOdd = False\n\n        for key in hm:\n            if hm[key] % 2 == 0:\n                ans += hm[key]\n            else:\n                ans += hm[key]\n                if not isFirstOdd:\n                    isFirstOdd = True\n                else:\n                    ans -= 1\n\n        return ans\n```\n\n___\nComplexity Analysis\n___\n* **Time Complexity :-** BigO(N)\n* **Space Complexity :-** BigO(K)\n\n___\n___\n\n**Approach - 2**\n\nSo, can we solve this problem without using **HASHMAP??**\n\nYes, we can do that.\n\nIn the problem, we have given that the all possible characters can be from\n* **a -> z**\n* **A -> Z**\n\n> Always, remember the ASCII value of **A** is 65! and if we add from here **26**, we will get the ASCII value of all the characters till **Z**\n> > But, if we add **32** to **65**, we will get **97**, from here all the lower case characters will start from **a**\n\nSo, instead of saving there frequency in **HashMap**, let\'s save it in array!\n* Let\'s create 2 array\n* Array one will be of **lowercase** of size **26**\n* Array one will be of **uppercase** of size **26**\n![image](https://assets.leetcode.com/users/images/c1aeea07-599a-4104-9677-ff6e5cc8a50e_1717463837.8300812.png)\n\nLet\'s have one example\n```\ns = "aaBBaa"\n```\n* So, we get `a` we will subtract it from **97** and if the diff is greater than or equals to **0**, we will update it\'s frequency\n* to **1**\n![image](https://assets.leetcode.com/users/images/baca1ba3-6729-4717-8773-acecd31af126_1717464028.5790937.png)\n* Again, we get `a` we will subtract it from **97** and if the diff is greater than or equals to **0**, we will update it\'s frequency\n* by **1**\n![image](https://assets.leetcode.com/users/images/0f9ccf6d-22ce-4947-b754-dd06b157333e_1717464112.6050699.png)\n* Now, we get `B` we will subtract it from **97** and if the diff is lesser than **0**, we will get to know that it is a **uppercase** character.\n* So, we will subtract `\'B\' - 65`, which will be `66 - 65 => 1`\n* We will update its frequency to 1 in the **uppercase** array\n![image](https://assets.leetcode.com/users/images/7924d340-f45a-4fa6-9ab5-d43f98173c43_1717464363.7993195.png)\n\nDoing this, we have optimise it a little, more! Now let\'s look at it\'s code\n\nLet\'s code it UP!\n\n**C++**\n```\nclass Solution {\npublic:\n    int longestPalindrome(std::string s) {\n        std::vector<int> lowerCase(26, 0);\n        std::vector<int> upperCase(26, 0);\n\n        for(int i = 0; i < s.length(); i++) {\n            char c = s[i];\n            if(c - \'a\' >= 0)\n                lowerCase[c - \'a\']++;\n            else\n                upperCase[c - \'A\']++;\n        }\n\n        int ans = 0;\n        bool isFirstOdd = false;\n\n        for(int i = 0; i < 26; i++) {\n            if(lowerCase[i] % 2 == 0) {\n                ans += lowerCase[i];\n            } else {\n                if(!isFirstOdd) {\n                    ans += lowerCase[i];\n                    isFirstOdd = true;\n                } else {\n                    ans += lowerCase[i] - 1;\n                }\n            }\n        }\n\n        for(int i = 0; i < 26; i++) {\n            if(upperCase[i] % 2 == 0) {\n                ans += upperCase[i];\n            } else {\n                if(!isFirstOdd) {\n                    ans += upperCase[i];\n                    isFirstOdd = true;\n                } else {\n                    ans += upperCase[i] - 1;\n                }\n            }\n        }\n        return ans;\n    }\n};\n```\n**JAVA**\n```\nclass Solution {\n    public int longestPalindrome(String s) {\n        int lowerCase[] = new int[26];\n        int upperCase[] = new int[26];\n        \n        for(int i =0; i <s.length(); i++){\n             char c = s.charAt(i);\n            if(c-97 >=0){\n                lowerCase[c-97]++;\n            }else{\n                upperCase[c-65]++;\n            }\n                \n        } \n        \n        int ans = 0;\n        boolean isFirstOdd   = false;    \n        for(int i = 0; i <26; i++){\n           if(lowerCase[i]%2 ==0){\n               ans += lowerCase[i];\n           }else{\n               if(isFirstOdd == false){\n                 ans += lowerCase[i];      \n                 isFirstOdd = true;\n               }\n               else\n                ans += lowerCase[i]-1;\n           }\n        }\n        \n        \n        \n        for(int i = 0; i <26; i++){\n        if(upperCase[i]%2 ==0){\n               ans += upperCase[i];\n           }else{\n               if(isFirstOdd == false){\n                 ans += upperCase[i];      \n                 isFirstOdd = true;\n               }\n               else\n                ans += upperCase[i]-1;\n           }\n        }\n       return ans;\n    }\n}\n```\n**PYTHON**\n```\nclass Solution:\n    def longestPalindrome(self, s: str) -> int:\n        lowerCase = [0] * 26\n        upperCase = [0] * 26\n        \n        for c in s:\n            if ord(c) - 97 >= 0:\n                lowerCase[ord(c) - 97] += 1\n            else:\n                upperCase[ord(c) - 65] += 1\n                \n        ans = 0\n        isFirstOdd = False\n        \n        for i in range(26):\n            if lowerCase[i] % 2 == 0:\n                ans += lowerCase[i]\n            else:\n                if not isFirstOdd:\n                    ans += lowerCase[i]\n                    isFirstOdd = True\n                else:\n                    ans += lowerCase[i] - 1\n        \n        for i in range(26):\n            if upperCase[i] % 2 == 0:\n                ans += upperCase[i]\n            else:\n                if not isFirstOdd:\n                    ans += upperCase[i]\n                    isFirstOdd = True\n                else:\n                    ans += upperCase[i] - 1\n        \n        return ans\n```\n\n___\nComplexity Analysis\n___\n* **Time Complexity :-** BigO(N)\n* **Space Complexity :-** BigO(1)
['C', 'Python', 'Java']