☠💀💯Faster✅💯Lesser🧠🎯C++✅Python3✅Java✅C✅Python✅C#✅💥🔥💫Explained with pics ☠💥🔥Beats💯
Intuition\n\n\nhttps://leetcode.com/problems/count-the-number-of-special-characters-i/solutions/5052632/easiest-c-python3-c-java-python-c-beats-simplified-versi
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/80791f80-346a-4caa-a0ac-9b863bc13043_1714741902.5949588.jpeg)\n![Screenshot 2024-05-03 184130.png](https://assets.leetcode.com/users/images/e6e8cde6-1ea8-4534-a301-d52bc37f7ed7_1714742275.188709.png)\nhttps://leetcode.com/problems/count-the-number-of-special-characters-i/solutions/5052632/easiest-c-python3-c-java-python-c-beats-simplified-version\nThis is one of The Similar question to this one Please do visit this solution as well..\n# **Hi I\'m Anuj Bhati from India.**\n# Here i\'m sharing my Solution with you with the best explanation i can.\n**Explanation**\n1. **Step - 1 :-** Initailaize two Arrays (sArr, cArr) respectively to store the lowercase letters in sArr & uppercase letters in cArr..\nint sArr[26] = {};\nint cArr[26] = {};\nthis intiated that every element in this 0 initally...\n---\n2. **Step - 2 :-** Now Start traversing through the string and each time you encounter a lowerCase increase the frequency in sArr at specific position associated with that letter by doing this sArr[s[i] - \'a\']++;\n---\n3. **Step - 3 :-** After traversing along the whole string we get the frequencies of each letter in it\'s respective array form..\n   For ex - s = "lEeTcOdE"; we get\n- sArr --> $$a=0,b=0,c=1,d=1,e=1,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,v=0,w=0,x=0,y=0,z=0$$\n- cArr --> $$A=0,B=0,C=0,D=0,E=2,F=0,G=0,H=0,I=0,J=0,K=0,L=0,M=0,N=0,O=1,P=0,Q=0,R=0,S=0,T=1,U=0,V=0,W=0,X=0,Y=0,Z=0$$\n---\n4. **Step - 4 :-** Now Start traversing through the arrays from right to left 25th index - 0th index, and check at any index if in both arrays there is a element present then return that character associated with that index..\n---\n5. **Step - 5 :-** The program is over now ... Please upvote if it\'s helpful for you guy\'s ... Do Upvote and Comment giving feedbacks to my solution...\n---\n\n```C++ []\n//Time Complexity - O(n)\n//Space Complexity - O(26) --> O(1)\nclass Solution {\npublic:\n    string greatestLetter(string s) {\n        int capArr[26] = {};\n        int smaArr[26] = {};\n        for (int i = 0; s[i] != \'\\0\'; ++i) {\n            if (s[i] >= \'a\' && s[i] <= \'z\')\n                smaArr[s[i] - \'a\']++;\n            else if (s[i] >= \'A\' && s[i] <= \'Z\')\n                capArr[s[i] - \'A\']++;\n        }\n        string ans = "";\n        for (int i = 25; i >= 0; --i) {\n            if (capArr[i] > 0 && smaArr[i] > 0) {\n                ans.push_back(i + \'A\');\n                break;\n            }\n        }\n        return ans;\n    }\n};\n```\n```Python3 []\n"""\nTime Complexity - O(n)\nSpace Complexity - O(1)\n"""\nclass Solution:\n    def greatestLetter(self, s: str) -> str:\n        dic = {}\n        res = ""\n        for i in s:\n            if i >= "A" and i <= "Z":\n                if i in dic and dic[i] % 2 == 1:\n                    dic[i] += 2\n                elif i not in dic:\n                    dic[i] = 2\n            if i >= "a" and i <= "z":\n                i = i.upper()\n                if i in dic and dic[i] % 2 == 0:\n                    dic[i] += 1\n                elif i not in dic:\n                    dic[i] = 1\n        for i in dic:\n            if dic[i] >= 3:\n                res = max(i, res)\n        return res\n```\n```Java []\nclass Solution {\n    public String greatestLetter(String s) {\n        int capArr[] = new int[26];\n        int smaArr[] = new int[26];\n        for (int i = 0; i < s.length(); ++i) {\n            if (s.charAt(i) >= \'a\' && s.charAt(i) <= \'z\')\n                smaArr[s.charAt(i) - \'a\']++;\n            else if (s.charAt(i) >= \'A\' && s.charAt(i) <= \'Z\')\n                capArr[s.charAt(i) - \'A\']++;\n        }\n        String Ans = "";\n        for (int i = 25; i >= 0; --i) {\n            if (capArr[i] > 0 && smaArr[i] > 0) {\n                Ans += (char) (i + \'A\');\n                break;\n            }\n        }\n        return Ans;\n    }\n}\n```\n```C []\n//TC - O(n)\n//SC - O(26) --> O(1)\nchar* greatestLetter(char* s) {\n    int capArr[26] = {};\n    int smaArr[26] = {};\n    for (int i = 0; s[i] != \'\\0\'; ++i) {\n        if (s[i] >= \'a\' && s[i] <= \'z\')\n            smaArr[s[i] - \'a\']++;\n        else if (s[i] >= \'A\' && s[i] <= \'Z\')\n            capArr[s[i] - \'A\']++;\n    }\n    char* x = (char*)malloc(sizeof(char) * 2);\n    x[1] = \'\\0\';\n    for (int i = 25; i >= 0; --i) {\n        if (capArr[i] > 0 && smaArr[i] > 0) {\n            x[0] = (i + \'A\');\n            break;\n        }\n        x[0] = \'\\0\';\n    }\n    return x;\n}\n```\n```Python []\nclass Solution(object):\n    def greatestLetter(self, s):\n        """\n        :type s: str\n        :rtype: str\n        """\n        # get all the unique ele - both upper case and lower case will appear\n        letters=set(s)\n\n        # sort and iterate in reverse mode\n        for ltr in sorted(letters, reverse=True):\n            if ltr.isupper() and ltr.lower() in letters:\n                return ltr\n\n        return \'\'\n\n# time = O(n^2)\n# space = 0(n)\n        \n```\n```C# []\npublic class Solution {\n    public string GreatestLetter(string s) {\n        var upper = 0;\n        var lower = 0;\n        foreach (var c in s){\n            upper |= char.IsUpper(c) ? 1 << c : 0;\n            lower |= char.IsLower(c) ? 1 << c : 0;\n        }\n        var r = int.Log2(upper & lower);\n        return new string((char)(\'A\' + r - 1), int.Sign(r));\n    }\n}\n```\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n***Very Easy Approach just count the number of lowerCase letters and store their  corresponding frequency in a smllArr of size 26 .. do this same with upperCase the start loop from the last index and check at that index any element is present in array in both small and largeArr if this return the UpperCase letter corresponds to that index***\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n     O(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n     O(26)\n# Code\n```\nclass Solution {\npublic:\n    string greatestLetter(string s) {\n        int capArr[26] = {};\n        int smaArr[26] = {};\n        for (int i = 0; s[i] != \'\\0\'; ++i) {\n            if (s[i] >= \'a\' && s[i] <= \'z\')\n                smaArr[s[i] - \'a\']++;\n            else if (s[i] >= \'A\' && s[i] <= \'Z\')\n                capArr[s[i] - \'A\']++;\n        }\n        string ans = "";\n        for (int i = 25; i >= 0; --i) {\n            if (capArr[i] > 0 && smaArr[i] > 0) {\n                ans.push_back(i + \'A\');\n                break;\n            }\n        }\n        return ans;\n    }\n};\n```\n# Please do upvote my solution\n![th.jpeg](https://assets.leetcode.com/users/images/b71c4422-57f7-4d8c-b101-f78c2beb9a48_1714744527.8043566.jpeg)\n
['Hash Table', 'String', 'Bit Manipulation', 'C', 'Enumeration', 'Python', 'C++', 'Java', 'Python3', 'C#']