question_slug stringlengths 3 77 | title stringlengths 1 183 | slug stringlengths 12 45 | summary stringlengths 1 160 ⌀ | author stringlengths 2 30 | certification stringclasses 2
values | created_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | updated_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | hit_count int64 0 10.6M | has_video bool 2
classes | content stringlengths 4 576k | upvotes int64 0 11.5k | downvotes int64 0 358 | tags stringlengths 2 193 | comments int64 0 2.56k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
first-letter-to-appear-twice | using Hashtable | using-hashtable-by-ganjinaveen-1ns0 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | GANJINAVEEN | NORMAL | 2023-02-28T19:16:17.010227+00:00 | 2023-02-28T19:16:17.010268+00:00 | 889 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def repeatedCharacter(self, s: str) -> str:\n map=defaultdict(int)\n for i in s:\n map[i]+=1\n if map[i]>=2:\n return i\n\n \n \n``` | 4 | 0 | ['Python3'] | 1 |
first-letter-to-appear-twice | Java Easiest Solution | java-easiest-solution-by-janhvi__28-n9th | \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# | Janhvi__28 | NORMAL | 2022-12-08T17:40:14.606893+00:00 | 2022-12-08T19:03:10.068831+00:00 | 401 | false | \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```\nclass Solution {\n public char repeatedCharacter(String s) {\n int[] cnt = new int[26];\n for (char c : s.toCharArray()) {\n if (++cnt[c - \'a\'] == 2) {\n return c;\n }\n }\n return \'.\';\n }\n \n}\n``` | 4 | 0 | ['Java'] | 0 |
first-letter-to-appear-twice | Java and C++ solution without using map/set | java-and-c-solution-without-using-mapset-5hj5 | Java solution:\n\npublic char repeatedCharacter(String s) {\n int freq[]=new int[26]; //26 size array to store the occurrence of lowercase characters | dmanocha464 | NORMAL | 2022-09-14T06:46:31.951872+00:00 | 2022-09-14T06:47:03.592583+00:00 | 658 | false | Java solution:\n```\npublic char repeatedCharacter(String s) {\n int freq[]=new int[26]; //26 size array to store the occurrence of lowercase characters \n int temp;\n for(int i=0;i<s.length();i++){\n temp=s.charAt(i)-\'a\'; //will give 0 to 25 for a to z \n if(freq[temp]==1){ //already 1 means repeated \n return s.charAt(i);\n }\n freq[temp]=1; //set value 1 if it has occured for the first time\n }\n return \' \';\n }\n```\nC++ solution:\n```\nchar repeatedCharacter(string s) {\n int freq[26]={0}; //26 size array to store the occurrence of lowercase characters \n int temp;\n for(int i=0;i<s.size();i++){\n temp=s[i]-\'a\'; //will give 0 to 25 for a to z \n if(freq[temp]==1){ //already 1 means repeated \n return s[i];\n }\n freq[temp]=1; //set value 1 if it has occured for the first time\n }\n return \' \';\n }\n``` | 4 | 0 | ['String', 'C', 'Java'] | 0 |
first-letter-to-appear-twice | 100% fastest CPP solution using hashing and ascii values | 100-fastest-cpp-solution-using-hashing-a-cozy | \nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n\t\tint n = s.length(); //string length\n int arr[26]={0}; //created an array | sahil1208 | NORMAL | 2022-08-18T18:24:52.014238+00:00 | 2022-08-18T18:30:55.116309+00:00 | 154 | false | ```\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n\t\tint n = s.length(); //string length\n int arr[26]={0}; //created an array for 26 alphabets from \'a\' to \'z\' and initialize with 0\n for(int i=0;i<n;i++){ //Iterating over given string\n int num = s[i]-\'a\'; //subtracting \'a\' from character such that we will get respective \n\t\t\t\t\t\t\t\t\t//index from 0 to 25 for every character present in string from \'a\' to \'z\'\n\t\t\tarr[num]++; //increasing value by 1 of character -> s[i] \n if(arr[num] == 2){ //checking if the index value is 2 of character s[i] \n return s[i]; //returning character s[i]\n }\n }\n return -1;\n }\n};\n```\nHope you find this solution helpful, if so then pls upvote it...\u270C\uD83C\uDFFB\uD83E\uDD17 | 4 | 0 | ['C'] | 0 |
first-letter-to-appear-twice | Java || HashMap || V. Easy Code || Easy 2 Understand | java-hashmap-v-easy-code-easy-2-understa-wbiq | class Solution {\n\n public char repeatedCharacter(String s) {\n HashMap hm = new HashMap<>();\n int idx = 0;\n for(int i=0;i<s.length() | Muskan_Malhotra | NORMAL | 2022-08-16T16:52:30.553627+00:00 | 2022-08-16T16:52:30.553668+00:00 | 685 | false | class Solution {\n\n public char repeatedCharacter(String s) {\n HashMap<Character,Integer> hm = new HashMap<>();\n int idx = 0;\n for(int i=0;i<s.length();i++){\n char c = s.charAt(i);\n if(hm.containsKey(c) ){\n \n idx = i;\n break;\n \n \n }\n else{\n hm.put(c,1);\n }\n }\n System.out.println(hm);\n \n \n return s.charAt(idx);\n }\n}\n | 4 | 0 | ['Java'] | 1 |
first-letter-to-appear-twice | Python (Simple Set) | python-simple-set-by-rnotappl-6ode | \n def repeatedCharacter(self, s):\n seen = set()\n \n for i in s:\n if i not in seen:\n seen.add(i)\n | rnotappl | NORMAL | 2022-08-16T14:24:48.475868+00:00 | 2022-08-16T14:24:48.475910+00:00 | 307 | false | \n def repeatedCharacter(self, s):\n seen = set()\n \n for i in s:\n if i not in seen:\n seen.add(i)\n else:\n return i\n | 4 | 0 | [] | 0 |
first-letter-to-appear-twice | Python | Easy Solution✅ | python-easy-solution-by-gmanayath-yk8r | \ndef repeatedCharacter(self, s: str) -> str:\n st = set()\n for i in range(len(s)): # s = "abccbaacz"\n if s[i] in st: # condition wi | gmanayath | NORMAL | 2022-08-16T13:12:54.480565+00:00 | 2022-12-22T16:49:14.193121+00:00 | 983 | false | ```\ndef repeatedCharacter(self, s: str) -> str:\n st = set()\n for i in range(len(s)): # s = "abccbaacz"\n if s[i] in st: # condition will be true when i = 3\n return s[i] # return c\n else:\n st.add(s[i]) # st = {a,b,c}\n``` | 4 | 0 | ['Python', 'Python3'] | 0 |
first-letter-to-appear-twice | Simple Java solution 0ms faster than 100% | simple-java-solution-0ms-faster-than-100-1rpy | \nclass Solution {\n public char repeatedCharacter(String s) \n {\n HashSet<Character> set = new HashSet<>();\n char ch;\n for(int i= | nomaanansarii100 | NORMAL | 2022-08-16T10:03:58.446917+00:00 | 2022-08-16T10:03:58.446947+00:00 | 488 | false | ```\nclass Solution {\n public char repeatedCharacter(String s) \n {\n HashSet<Character> set = new HashSet<>();\n char ch;\n for(int i=0; i<s.length();i++)\n {\n ch= s.charAt(i);\n \n if(set.contains(ch))\n return ch;\n \n else\n set.add(ch);\n }\n \n return \'a\'; //any random character because the string s will contain at least one character which repeats itself.\n }\n}\n``` | 4 | 0 | ['Java'] | 0 |
first-letter-to-appear-twice | Python Elegant & Short | O(n) time & O(1) space | Set | python-elegant-short-on-time-o1-space-se-y01x | \tdef repeatedCharacter(self, s: str) -> str:\n\t\tseen = set()\n\n\t\tfor c in s:\n\t\t\tif c in seen:\n\t\t\t\treturn c\n\t\t\tseen.add(c)\n | Kyrylo-Ktl | NORMAL | 2022-08-13T19:14:15.641911+00:00 | 2022-08-13T19:14:27.858099+00:00 | 543 | false | \tdef repeatedCharacter(self, s: str) -> str:\n\t\tseen = set()\n\n\t\tfor c in s:\n\t\t\tif c in seen:\n\t\t\t\treturn c\n\t\t\tseen.add(c)\n | 4 | 0 | ['Ordered Set', 'Python', 'Python3'] | 0 |
first-letter-to-appear-twice | Short JavaScript Solution Using a Set Object | short-javascript-solution-using-a-set-ob-dhvr | Found this solution helpful? Consider showing support by upvoting this post.\nHave a question? Kindly leave a comment below.\nThank you and happy hacking!\n\nva | sronin | NORMAL | 2022-07-24T16:18:39.348258+00:00 | 2022-07-25T05:08:54.096291+00:00 | 474 | false | Found this solution helpful? Consider showing support by upvoting this post.\nHave a question? Kindly leave a comment below.\nThank you and happy hacking!\n```\nvar repeatedCharacter = function(s) {\n let letterSet = new Set()\n \n for(let i = 0; i< s.length;i++){\n if(letterSet.has(s[i])) return s[i]\n letterSet.add(s[i]) \n }\n};\n``` | 4 | 0 | ['Ordered Set', 'JavaScript'] | 0 |
first-letter-to-appear-twice | O(n) time | O(1) | 100% faster | memory usage less than 100% | on-time-o1-100-faster-memory-usage-less-rdc35 | ```\n var repeatedCharacter = function(s) {\n const m = {};\n \n for(let i of s) {\n\t\tm[i] = m[i] + 1 || 1; \n \n if(m[i] == 2) {\n | asadullakkh | NORMAL | 2022-07-24T05:52:38.476161+00:00 | 2022-09-18T18:45:22.763134+00:00 | 579 | false | ```\n var repeatedCharacter = function(s) {\n const m = {};\n \n for(let i of s) {\n\t\tm[i] = m[i] + 1 || 1; \n \n if(m[i] == 2) {\n return i;\n }\n }\n}; | 4 | 0 | ['JavaScript'] | 2 |
first-letter-to-appear-twice | [Python3] mask | python3-mask-by-ye15-cufn | Please pull this commit for solutions of weekly 303. \n\n\nclass Solution:\n def repeatedCharacter(self, s: str) -> str:\n mask = 0 \n for ch i | ye15 | NORMAL | 2022-07-24T04:02:19.906399+00:00 | 2022-07-24T04:17:13.749188+00:00 | 427 | false | Please pull this [commit](https://github.com/gaosanyong/leetcode/commit/d61cd3ed09bbf59fd619802a6e861a516ec17094) for solutions of weekly 303. \n\n```\nclass Solution:\n def repeatedCharacter(self, s: str) -> str:\n mask = 0 \n for ch in s: \n if mask & 1<<ord(ch)-97: return ch \n mask ^= 1<<ord(ch)-97\n``` | 4 | 0 | ['Python3'] | 1 |
first-letter-to-appear-twice | simple bit-masking | interesting soln | checkout once... | linear time soln | simple-bit-masking-interesting-soln-chec-5iq8 | \n\n# Code\n\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n int p=0;\n for(auto it:s){\n if((p&(1<<(it-\'a\'))))r | shivamaurya1910 | NORMAL | 2024-02-15T11:09:51.659403+00:00 | 2024-02-15T11:09:51.659433+00:00 | 140 | false | \n\n# Code\n```\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n int p=0;\n for(auto it:s){\n if((p&(1<<(it-\'a\'))))return it;\n p=(p|(1<<(it-\'a\')));\n }\n return \'a\';\n }\n};\n``` | 3 | 0 | ['Bit Manipulation', 'C++'] | 0 |
first-letter-to-appear-twice | 💀☠️🔥Easiest Fastest 🔥 Beats 💯.C++ 🔥 Python3 🐍🔥Java 🔥C 🔥 Python🐍🔥Solution Explained 🧠🎯🏆 | easiest-fastest-beats-c-python3-java-c-p-55md | Intuition\n\n\n\n\nC++ []\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n unordered_map<char, int> mp;\n for (auto& c : s) {\ | Edwards310 | NORMAL | 2024-02-10T08:04:15.436467+00:00 | 2024-04-18T03:51:23.012762+00:00 | 150 | false | # Intuition\n\n\n\n\n```C++ []\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n unordered_map<char, int> mp;\n for (auto& c : s) {\n mp[c]++;\n if (mp[c] >= 2)\n return c;\n }\n return \'-1\';\n }\n};\n```\n```Python3 []\nclass Solution:\n def repeatedCharacter(self, s: str) -> str:\n dict = {}\n for x in s:\n if x in dict:\n dict[x] += 1\n if dict[x] == 2:\n return x\n else:\n dict[x] = 1\n\n return "-1"\n\n```\n```C++ []\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n char x = \'-1\';\n for (int i = 0; i < s.length() - 1; i++) {\n int idx1 = s.at(i) - \'a\', idx2 = s.at(i + 1) - \'a\';\n if ((idx1 ^ idx2) == 0) {\n x = s.at(i);\n break;\n }\n }\n return x;\n }\n};\n```\n```Java []\nclass Solution {\n public char repeatedCharacter(String s) {\n int[] arr = new int[26];\n for (int i = 0; i < s.length(); i++) {\n int idx = s.charAt(i) - \'a\';\n arr[idx]++;\n if (arr[idx] == 2)\n return s.charAt(i);\n }\n return \'z\';\n }\n}\n```\n```Python []\nclass Solution(object):\n def repeatedCharacter(self, s):\n """\n :type s: str\n :rtype: str\n """\n list = [0] * 26\n for x in s: \n list[ord(x) - ord(\'a\')] += 1\n if list[ord(x) - ord(\'a\')] == 2:\n return x\n return \'-1\'\n```\n```C++ []\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n unordered_map<char, int> mp;\n for (auto& c : s) {\n if (!(mp.contains(c)))\n mp.insert({c, 1});\n else {\n mp[c]++;\n for (auto& p : mp) {\n if (p.second >= 2)\n return p.first;\n }\n }\n }\n return \'-1\';\n }\n};\n```\n```C []\nchar repeatedCharacter(char* s) {\n int arr[26] = {};\n for (int i = 0; s[i] != \'\\0\'; i++) {\n int idx = s[i] - \'a\';\n arr[idx]++;\n if (arr[idx] == 2)\n return s[i];\n }\n return \'#\';\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. -->\nSimple Approach just iterate over a character store it\'s frequency in a map//dictionary and check if it\'s frequency is now 2 if it\'s return the current letter\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n O(n) -- n = string length \n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n O(26) -- O(1)\n# Code\n```\nchar repeatedCharacter(char* s) {\n int arr[26] = {};\n for (int i = 0; s[i] != \'\\0\'; i++) {\n int idx = s[i] - \'a\';\n arr[idx]++;\n if (arr[idx] == 2)\n return s[i];\n }\n return \'#\';\n}\n```\n\n | 3 | 0 | ['Hash Table', 'String', 'Bit Manipulation', 'C', 'Counting', 'Python', 'C++', 'Java', 'Python3'] | 2 |
first-letter-to-appear-twice | Java || 100% beats || HashSet || ❤️ | java-100-beats-hashset-by-saurabh_kumar1-7hy7 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | saurabh_kumar1 | NORMAL | 2023-08-22T15:40:58.305194+00:00 | 2023-08-22T15:40:58.305216+00:00 | 380 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:0(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:0(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public char repeatedCharacter(String s) {\n Set<Character> set = new HashSet<>();\n char ans = s.charAt(0);\n for(int i=1; i<s.length(); i++){\n set.add(s.charAt(i-1));\n if(set.contains(s.charAt(i))){\n ans = s.charAt(i);\n break;\n } \n }\n return ans;\n }\n}\n``` | 3 | 0 | ['Java'] | 1 |
first-letter-to-appear-twice | Easiest solution in python | easiest-solution-in-python-by-mohammad_t-8ki9 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Mohammad_tanveer | NORMAL | 2023-02-28T15:20:22.945211+00:00 | 2023-02-28T15:20:22.945261+00:00 | 1,384 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def repeatedCharacter(self, s: str) -> str:\n list1=[]\n for i in range(len(s)):\n if s[i] in list1:\n return s[i]\n else:\n list1.append(s[i])\n \n``` | 3 | 0 | ['Python3'] | 1 |
first-letter-to-appear-twice | C++ approach O(N) Beats 100% || With Explanation || Without hash | c-approach-on-beats-100-with-explanation-keea | Intuition\n Describe your first thoughts on how to solve this problem. \nCreate a count array which store frequency of all character of string \nCharcter whose | Utkarsh_Raghuvanshi | NORMAL | 2023-02-17T19:35:39.511806+00:00 | 2023-02-17T19:35:39.511832+00:00 | 787 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nCreate a count array which store frequency of all character of string \nCharcter whose frequency becomes 2 return that character \n# Approach\n1> We simply make a count array which store frequency of all character of string \n2> As soon as we see the character we increase the value of count\n3> when 1st time count[of particular character]==2 we return that character \n\nDRY RUN \n\ninitially count array is 0\n\ns="abcddb"\n\ni=0 : \ncount[\'a\']=1;\ni=1:\ncount[\'b\']=1;\ni=2:\ncount[\'c\']=1;\ni=3:\ncount[\'d\']=1;\ni=4:\ncount[\'d\']=2\n\nAs soon as count[s[i]]==2 return s[i]\n\ncount[s[i]]--> count[\'d\']==2 \nreturn d;\n\n\nif suppose no repeating character return -1 (which is not possible in this question)\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(256) or O(26)\n\n256 -> it will check for all ASCII character \n26-> Only for lowercase \n\n# Code\n```\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n int count[256]={0};\n //we can take 26 also as char are in lowercase \n for(int i=0;i<s.length();i++)\n {\n count[s[i]]++;\n if(count[s[i]]==2)\n return s[i];\n }\n return -1;\n }\n};\n``` | 3 | 0 | ['C++'] | 2 |
first-letter-to-appear-twice | ✅ *HASHMAP*|*HASHSET* | *Easiest One*|*Java*| please Upvote if it helps🆙👆 | hashmaphashset-easiest-onejava-please-up-tst3 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | shivanigam | NORMAL | 2023-01-23T08:19:58.327208+00:00 | 2023-01-23T08:19:58.327254+00:00 | 968 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\no(n)\n- Space complexity:\no(n)\n# Code\n```\nclass Solution {\n public char repeatedCharacter(String s) {\n char ch=\' \';\n HashMap<Character, Integer> map\n = new HashMap<Character, Integer>();\n for(int i=0;i<s.length();i++){\n if(map.containsKey(s.charAt(i)))\n {\n ch=s.charAt(i);\n break;\n }\n map.put(s.charAt(i),i);\n }\n return ch;/*\n //HashMap is faster than HashSet\n // both are unordered\n char ch=\' \';\n HashSet<Character> set\n = new HashSet<Character>();\n for(int i=0;i<s.length();i++){\n if(set.contains(s.charAt(i)))\n {\n ch=s.charAt(i);\n break;\n }\n set.add(s.charAt(i));\n }\n return ch;*/\n }\n}\n``` | 3 | 0 | ['Java'] | 0 |
first-letter-to-appear-twice | ✅Fastest C++ Solution using Hashing | fastest-c-solution-using-hashing-by-adar-3crt | \n# Code\n\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n\n char ans = \'a\';\n\n // Hashing \n unordered_map<char,int> | Adarsh_Shukla | NORMAL | 2022-12-23T12:33:47.244229+00:00 | 2022-12-23T12:33:47.244273+00:00 | 518 | false | \n# Code\n```\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n\n char ans = \'a\';\n\n // Hashing \n unordered_map<char,int> mp;\n for(int i=0;i<s.length();++i){\n mp[s[i]]++;\n if(mp[s[i]]==2) {\n ans=s[i];\n break;\n }\n }\n\n return ans;\n\n }\n};\n``` | 3 | 0 | ['C++'] | 0 |
first-letter-to-appear-twice | JavaScript using a Set O(1) Time and Space complexity | 92% | javascript-using-a-set-o1-time-and-space-pmjk | We use a Set to keep track of what letters we have already seen. Once we find a letter that is already in our Set we know we found a duplicate letter.\n\nMax 26 | Golyator | NORMAL | 2022-11-28T20:02:32.285226+00:00 | 2022-12-14T21:08:41.333177+00:00 | 349 | false | We use a Set to keep track of what letters we have already seen. Once we find a letter that is already in our Set we know we found a duplicate letter.\n\nMax 26 unique lowercase letters:\nTime: O(1)\nSpace: O(1)\n\nI saw many solutions here claiming a O(n) space and time complexity but they aint correct, it is O(1). There are at max 26 letters so at max we have 26 uniqe letters and then the 27th will be a duplicate. So we will never do more than 27 iterations no matter how long the string is and never have more than 26 Chars in our Set which makes time and space complexity constant. \n\n````\nvar repeatedCharacter = function(s) {\n let seen = new Set();\n for (let a of s) {\n if (seen.has(a)) return a;\n else seen.add(a);\n }\n return \'\';\n};\n```` | 3 | 0 | ['Ordered Set', 'JavaScript'] | 1 |
first-letter-to-appear-twice | ✨🚀Easiest approach|| ONLY 3 steps || beginner friendly 🚀 Easiest C++ Solution | easiest-approach-only-3-steps-beginner-f-n2b8 | Only 3 Steps\n1. Declare an hashmap\n2. In a loop insert string[i] and keep increasing map count \n3. If its count is more than 1 that means its double hence br | shxbh | NORMAL | 2022-11-26T18:52:48.879288+00:00 | 2022-11-26T18:52:48.879316+00:00 | 244 | false | ## Only 3 Steps\n1. Declare an hashmap\n2. In a loop insert string[i] and keep increasing map count \n3. If its count is more than 1 that means its double hence break and return the char\n```\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n char c;\n\t\t//Declare an hashmap\n map<char,int> m;\n\t\t//In a loop insert string[i] and keep increasing map count \n for(auto it:s){\n m[it]++;\n\t\t\t//If its count is more than 1 that means its double hence break and return the char\n if(m[it]>1){\n c = it;\n break;\n }\n }\n return c;\n \n }\n};\n``` | 3 | 0 | ['C'] | 0 |
first-letter-to-appear-twice | Java Faster than 100% (3 Solutions) | java-faster-than-100-3-solutions-by-adit-fq8y | Solution 1: Faster than 100%\n\nclass Solution {\n public char repeatedCharacter(String s) {\n for(int i=1; i<s.length(); i++){\n if(s.inde | aditigupta2048 | NORMAL | 2022-10-09T08:12:35.678088+00:00 | 2022-10-09T08:12:35.678138+00:00 | 1,176 | false | Solution 1: Faster than 100%\n```\nclass Solution {\n public char repeatedCharacter(String s) {\n for(int i=1; i<s.length(); i++){\n if(s.indexOf(s.charAt(i))!=i){\n return s.charAt(i);\n }\n }\n return \' \';\n }\n}\n```\n\nSolution 2: Faster than 100%\n```\nclass Solution {\n public char repeatedCharacter(String s) { \n for(int i=1; i<s.length(); i++){\n for(int j=0; j<i; j++){\n if(s.charAt(i) == s.charAt(j))\n return s.charAt(i);\n }\n }\n return \' \';\n }\n}\n```\n\nSolution 3: Faster than 14%\n```\nclass Solution {\n public char repeatedCharacter(String s) { \n for(int i=1; i<s.length(); i++){\n if(s.substring(0,i).contains(String.valueOf(s.charAt(i))))\n return s.charAt(i);\n }\n return \' \';\n }\n}\n``` | 3 | 0 | ['Java'] | 0 |
first-letter-to-appear-twice | Python Set | Easy understanding | python-set-easy-understanding-by-prithul-r0sn | Idea: We don\'t need to loop totally to get the answer. We just keep track of what letters went ahead by using a set. If any letter is repeated, return the lett | prithuls | NORMAL | 2022-08-16T01:26:36.350655+00:00 | 2022-08-16T01:26:36.350684+00:00 | 269 | false | Idea: We don\'t need to loop totally to get the answer. We just keep track of what letters went ahead by using a set. If any letter is repeated, return the letter.\n```\nclass Solution:\n def repeatedCharacter(self, s: str) -> str:\n hm = set()\n for s_ in s:\n if s_ not in hm:\n hm.add(s_)\n else: return s_\n```\nPlease upvote if you like my solution. Thanks. | 3 | 0 | ['Ordered Set', 'Python'] | 0 |
first-letter-to-appear-twice | python3 code | python3-code-by-blackhole_tiub-ztak | \nvar repeatedCharacter = function(s) {\n let arr = []\n for (let i of s) {\n if (!arr.includes(i)) {\n arr.push(i)\n }\n | Little_Tiub | NORMAL | 2022-07-31T18:26:47.154117+00:00 | 2023-05-06T17:58:41.072742+00:00 | 62 | false | ```\nvar repeatedCharacter = function(s) {\n let arr = []\n for (let i of s) {\n if (!arr.includes(i)) {\n arr.push(i)\n }\n else {\n return i\n }\n }\n}\n``` | 3 | 1 | ['JavaScript'] | 0 |
first-letter-to-appear-twice | c++ || easy || map | c-easy-map-by-abhijoy123-lkr3 | \nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n map<char, int> mp;\n for(int i=0;i<s.length();i++)\n {\n m | abhijoy123 | NORMAL | 2022-07-29T20:26:05.940905+00:00 | 2022-07-29T20:26:05.940942+00:00 | 265 | false | ```\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n map<char, int> mp;\n for(int i=0;i<s.length();i++)\n {\n mp[s[i]]++;\n if(mp[s[i]]==2)\n return s[i];\n }\n return 0;\n }\n};\n```\n\nplease upvote!! | 3 | 0 | ['C', 'C++'] | 1 |
first-letter-to-appear-twice | Python3 O(n) || O(1) # Runtime: 30ms 90.00% || Memory: 13.9mb 10.00% | python3-on-o1-runtime-30ms-9000-memory-1-8i2s | \nclass Solution:\n# O(n) || O(1)\n# Runtime: 30ms 90.00% || Memory: 13.9mb 10.00%\n def repeatedCharacter(self, string: str) -> str:\n strAlphaFr | arshergon | NORMAL | 2022-07-26T03:38:00.703397+00:00 | 2022-07-26T03:38:00.703428+00:00 | 507 | false | ```\nclass Solution:\n# O(n) || O(1)\n# Runtime: 30ms 90.00% || Memory: 13.9mb 10.00%\n def repeatedCharacter(self, string: str) -> str:\n strAlphaFreq = [0] * 26\n\n for char in string:\n index = ord(char) - ord(\'a\')\n\n strAlphaFreq[index] += 1\n\n if strAlphaFreq[index] > 1:\n return char\n\n``` | 3 | 0 | ['Python', 'Python3'] | 0 |
first-letter-to-appear-twice | c++ solution O(1) space using bitmask | c-solution-o1-space-using-bitmask-by-dil-uh85 | \nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n int mask=0;\n int n=s.size();\n for(int i=0;i<n;i++)\n {\n | dilipsuthar17 | NORMAL | 2022-07-24T05:25:38.577891+00:00 | 2022-07-24T06:50:49.953937+00:00 | 200 | false | ```\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n int mask=0;\n int n=s.size();\n for(int i=0;i<n;i++)\n {\n if(mask&(1<<(s[i]-\'a\')))\n {\n return s[i];\n }\n else\n {\n mask|=(1<<(s[i]-\'a\'));\n }\n }\n return \'dilip\';\n }\n};\n``` | 3 | 0 | ['C', 'C++'] | 0 |
first-letter-to-appear-twice | easy short efficient code | easy-short-efficient-code-by-maverick09-d3pe | \nclass Solution {\n typedef long long ll;\n#define vi(x) vector<x>\npublic:\n char repeatedCharacter(string& s) {\n ll mask=0;\n char res = | maverick09 | NORMAL | 2022-07-24T04:09:02.291816+00:00 | 2022-07-24T04:09:02.291848+00:00 | 257 | false | ```\nclass Solution {\n typedef long long ll;\n#define vi(x) vector<x>\npublic:\n char repeatedCharacter(string& s) {\n ll mask=0;\n char res = \'.\';\n for (char ch : s) {\n if (mask&(1<<(ch-\'a\'))) {\n res = ch;\n break;\n }\n mask|=(1<<(ch-\'a\'));\n }\n return res;\n }\n};\n``` | 3 | 0 | ['C', 'Bitmask'] | 0 |
first-letter-to-appear-twice | EASSY JAVA SOLUTION BEATING 100% IN 0ms || USE OF HASHSETS | eassy-java-solution-beating-100-in-0ms-u-dymi | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | ADARSH_GAUTAM_576 | NORMAL | 2025-03-29T06:24:58.074181+00:00 | 2025-03-29T06:24:58.074181+00:00 | 70 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public char repeatedCharacter(String s) {
List<Character> ans = new ArrayList<>();
for(int i=0;i<s.length();i++){
for(int j=0;j<ans.size();j++){
if(s.charAt(i)==ans.get(j)){
return s.charAt(i);
}
}
ans.add(s.charAt(i));
}
return s.charAt(0);
}
}
``` | 2 | 0 | ['Hash Table', 'String', 'Counting', 'Java'] | 0 |
first-letter-to-appear-twice | Optimal O(n) Solution || Easy to understand | optimal-on-solution-easy-to-understand-b-zdsy | IntuitionWe need to efficiently track the characters we have seen and detect when a character appears for the second time.ApproachWe can use a Set data structur | bibekbarik | NORMAL | 2025-03-28T14:07:01.604814+00:00 | 2025-03-28T14:07:01.604814+00:00 | 37 | false | # Intuition
We need to efficiently track the characters we have seen and detect when a character appears for the second time.
# Approach
We can use a Set data structure to keep track of characters we have already encountered.
1) Iterate through the string character by character.
2) Check if the character is already in the Set:
-> If yes, return it immediately (since it's the first duplicate).
I -> f no, add it to the Set and continue.
# Complexity
- Time complexity: O(n)
- Space complexity: O(1)
# Code
```python3 []
class Solution:
def repeatedCharacter(self, s: str) -> str:
charSet = set()
for char in s:
if char in charSet: return char
else: charSet.add(char)
``` | 2 | 0 | ['Python3'] | 0 |
first-letter-to-appear-twice | 🔥🔥best and easy solution in c++||beat 💯 ..0 ms.. simple✅📌 | best-and-easy-solution-in-cbeast-0-ms-si-tkxw | Code | Kaviyant7 | NORMAL | 2025-03-06T07:39:20.000099+00:00 | 2025-03-06T07:45:27.755342+00:00 | 63 | false |
# Code
```cpp []
class Solution {
public:
char repeatedCharacter(string s) {
unordered_map<char,int>m;
for(char c:s){
m[c]++;
if(m[c]==2) {
return c;
break;
}
}
return 0;
}
};
``` | 2 | 0 | ['C++'] | 0 |
first-letter-to-appear-twice | 0MS 🥇|| BEATS 100% 🥂 || 6 LINE CODE ⛳ || 🌟JAVA ☕ | 0ms-beats-100-6-line-code-java-by-galani-71cn | Code | Galani_jenis | NORMAL | 2025-01-18T08:46:45.497996+00:00 | 2025-01-18T08:46:45.497996+00:00 | 269 | false | 
# Code
```java []
class Solution {
public char repeatedCharacter(String s) {
Set<Character> set = new HashSet<>();
for (char ch : s.toCharArray()) {
if (set.contains(ch)) {
return ch;
}
set.add(ch);
}
return '0';
}
}
```

| 2 | 0 | ['Java'] | 0 |
first-letter-to-appear-twice | Beats 100% with O(1) Space | beats-100-with-o1-space-by-hebahamdan296-91he | Intuition\nTo find the first repeated character, I considered using a hash set or map. However, since the input consists of only lowercase letters, I opted for | hebahamdan296 | NORMAL | 2024-09-28T11:08:50.506452+00:00 | 2024-09-28T11:19:22.594980+00:00 | 22 | false | # Intuition\nTo find the first repeated character, I considered using a hash set or map. However, since the input consists of only lowercase letters, I opted for a vector of size 26. This approach is more efficient, as it uses constant space and avoids the overhead of dynamic data structures, making it ideal for tracking up to 26 characters.\n# Approach\n1- Initialize a Vector: I create a vector of size 26 (for each letter of the English alphabet) initialized to zero. This helps me track whether a character has been encountered.\n\n2- Iterate Through the String: I loop through each character in the string. For each character:\n\n - I calculate its index using the expression s[i] - \'a\', which converts the character to an index (0 for \'a\', 1 for \'b\', etc.).\n- I check if the character has been seen before by examining the value at the corresponding index in the vector.\n- If it hasn\'t been seen (value is 0), I mark it as seen by setting the value at that index to 1 and continue to the next character.\n- If it has been seen (value is 1), I return that character as the first repeated character.\n- Return Default: If no repeated character is found (though the problem guarantees there will be one), I return the first character of the string.\n\nThis approach allows me to efficiently identify the first repeated character with minimal overhead.\n\n# Complexity\n- Time complexity: **O(n)**\n- Space complexity: **O(1)** The space used by the vector is constant (26 elements), regardless of the size of the input string.\n\n# Code\n```cpp []\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n vector<int>letters(26,0);\nfor(int i=0;i<s.size();i++){\n int index=s[i]-\'a\';\n if(letters[index]==0){\n letters[index]=1;continue;\n }\n if(letters[index]==1){return s[i];}\n}return s[0];\n }\n};\n```\n | 2 | 0 | ['C++'] | 0 |
first-letter-to-appear-twice | 🚀Easy Solution✅ Beats100%🚀Beginner-Friendly Solution and Approach 💫 #JAVA ✅ Detailed Explanation✅ | easy-solution-beats100beginner-friendly-f67rg | Intuition\nTo find the first character that appears twice in a string, we keep track of characters we\'ve seen. When we see a character for the second time, we | kanix-1801 | NORMAL | 2024-06-24T17:13:54.120395+00:00 | 2024-06-24T17:13:54.120430+00:00 | 402 | false | # Intuition\n***To find the first character that appears twice in a string, we keep track of characters we\'ve seen. When we see a character for the second time, we shout, "Found you!" and return it.***\n\n\n# Approach\n- **Create a tracker :** Use an array with 26 slots (one for each letter of the alphabet) to track characters we\'ve seen.\n- **Go through the string :** Check each character in the string one by one.\n- **Check the tracker:** For each character, see if we\'ve seen it before using our array.\n- **Return if repeated:** If the character is already in our tracker, shout, "Found you!" and return it.\n- **Mark as seen:** If it\'s the first time we\'ve seen the character, mark it in our tracker.\n# Complexity\n- **Time complexity :** O(\uD835\uDC5B)\n--> where \uD835\uDC5B is the length of the string, because we are iterating through the string only once.\n\n- Space complexity: O(*1*)\n--> since the size of the boolean array is constant (26 elements).\n\n# Code\n```\nclass Solution {\n public char repeatedCharacter(String s) {\n boolean[] arr = new boolean[26]; // Array to keep track\n for(char x: s.toCharArray()) // Iterate through each character in the string\n if(arr[x - \'a\']) // Check if the character has been seen before\n return x; // If yes, return it\n else arr[x - \'a\'] = true; // If no, mark it as seen\n\n return \' \'; // Fallback (will never reach here as per problem statement)\n }\n}\n``` | 2 | 0 | ['String', 'Counting', 'Java'] | 0 |
first-letter-to-appear-twice | Easy solution | easy-solution-by-death-metal-d1uq | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Death-metal | NORMAL | 2024-05-31T17:16:55.563020+00:00 | 2024-05-31T17:16:55.563039+00:00 | 36 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def repeatedCharacter(self, s: str) -> str:\n store=[]\n for char in s:\n if char in store:\n return char\n else:\n store.append(char)\n \n``` | 2 | 0 | ['Array', 'String', 'Counting', 'Python3'] | 1 |
first-letter-to-appear-twice | Easy Java Solution || Beginner Friendly || HashMap | easy-java-solution-beginner-friendly-has-j6tj | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | anikets101 | NORMAL | 2024-04-23T01:25:53.282194+00:00 | 2024-04-23T01:25:53.282215+00:00 | 497 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public char repeatedCharacter(String s) {\n HashMap<Character,Integer>mp = new HashMap<>();\n for(int i=0;i<s.length();i++){\n mp.put(s.charAt(i),mp.getOrDefault(s.charAt(i),0)+1);\n if(mp.get(s.charAt(i))>=2)\n return s.charAt(i);\n }\n return \' \';\n\n }\n}\n``` | 2 | 0 | ['Java'] | 0 |
first-letter-to-appear-twice | HashMap Beats 100% solutions | hashmap-beats-100-solutions-by-shreya_ku-5n1d | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Shreya_Kumari_24 | NORMAL | 2024-04-04T12:34:25.877820+00:00 | 2024-04-04T12:34:25.877839+00:00 | 228 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public char repeatedCharacter(String s) {\n HashMap<Character,Integer>mp = new HashMap<>();\n for(int i=0;i<s.length();i++){\n mp.put(s.charAt(i),mp.getOrDefault(s.charAt(i),0)+1);\n if(mp.get(s.charAt(i))>=2)\n return s.charAt(i);\n }\n return \' \';\n\n }\n}\n``` | 2 | 0 | ['Java'] | 0 |
first-letter-to-appear-twice | Java | Easy Solution | 0ms | HashMap | java-easy-solution-0ms-hashmap-by-lophiu-ixcl | Intuition\n Describe your first thoughts on how to solve this problem. \nI thought of using a HashMap to find the first letter that occurs twice. If a letter oc | Lophius_ | NORMAL | 2024-03-05T09:02:39.835942+00:00 | 2024-03-05T09:08:34.325594+00:00 | 277 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI thought of using a HashMap to find the first letter that occurs twice. If a letter occurs for the first time, a HashMap will return $$null$$ if we try to use it as a key. If it does return a value, that means it\'s not occuring for the first time. So we return the first character which does not return null.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe first create a HashMap called map.\n\nWe also create a character array and use a for-each loop to iterate through it.\n\nA for-each loop is faster than a for loop, so I am using it instead of using a for loop with charAt().\nWe check for null when each character in the for loop is used as a key in the HashMap $$map$$.\nIf true, we enter the character into the map as a key with a placeholder value (the integer 0).\nWe use else and return the first character that does not satisfy the if statement.\n\nThe line $$return \' \';$$ is to avoid this error:\n$$Line 13: error: missing$$ $$return$$ $$statement$$\n$$ }$$\n$$ ^$$\n\n\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```\nclass Solution {\n public char repeatedCharacter(String s) {\n HashMap<Character,Integer> map = new HashMap<>();\n char[] ch = s.toCharArray();\n for(char c :ch){\n if(map.get(c)==null){\n map.put(c,0);\n }else{\n return c;\n }\n }\n return \' \';\n }\n}\n``` | 2 | 0 | ['Hash Table', 'Java'] | 2 |
first-letter-to-appear-twice | ✨ Simple Python3 Solution with Explanation - Beats 93% ✨ O(n) time ⏱ & O(1) space 🪐 | simple-python3-solution-with-explanation-a2oi | Intuition\nThe question asks us to find the first character that appears twice in the string s. A set would be helpful for us here, because it would allow us to | fionabronwen | NORMAL | 2024-02-22T01:36:54.311484+00:00 | 2024-02-22T02:05:52.050763+00:00 | 255 | false | # Intuition\nThe question asks us to find the first character that appears twice in the string `s`. A set would be helpful for us here, because it would allow us to check for the presence of a character in constant, O(1), time.\n# Approach\n1. Create an empty set called `seen`. We will use this to track which characters we have seen in `s`.\n2. Iterate through `s`. For each character we will check if it is present in `seen`. If the character is in `seen` then we have found a character that occurs twice! Return that character.\n3. Otherwise, add the current `char` to `seen` and continue on.\n4. Lastly, return an empty string. This will only occur if `s` does not contain a repeated character. On Leetcode, the problem constraints specify that `s` will always be a least length `2` and always have at least one repeated character, but for completeness it is good practice to return something in case of invalid input.\n\n# Complexity\n- Time complexity: $$O(n)$$\nExplanation: `seen` set creation $$O(n)$$ + iterating over `s` $$O(n)$$ = $$O(n)$$\n\n- Space complexity: $$O(1)$$\nExplanation: `seen` will only contain lowercase letters from `a-z`, therefore the length of `seen` will never be longer than 26 which is constant regardless of the length of `s`.\n\n# Code\n```\nclass Solution:\n def repeatedCharacter(self, s: str) -> str:\n seen = set()\n for char in s:\n if char in seen:\n return char\n seen.add(char)\n return \'\'\n``` | 2 | 0 | ['Python3'] | 1 |
first-letter-to-appear-twice | C - Bit Array - 0ms - 6.37 mb | c-bit-array-0ms-637-mb-by-m4rt_h-hngt | \n\n# Approach\nSince the input is limited to being a-z you could just have an array and store some integers in it and keep track of how many you encouter, and | m4rt_h | NORMAL | 2024-01-17T14:06:25.183517+00:00 | 2024-01-17T14:06:25.183552+00:00 | 50 | false | \n\n# Approach\nSince the input is limited to being a-z you could just have an array and store some integers in it and keep track of how many you encouter, and then return when you first encounter a seccond one. But since you only need to store 1 bit of information (if you have seen it before), you can use a bit array instead, so that\'s what I did.\n\nTo get the offset/index to the array/bitarray I decided to use some simple math, the ascii value for the current character minus the ascii value for `a`.\n\nTo go through the string I just used a simple while loop to keep on looping untill it reaches a `\\0` character.\n\nThen I just use some simple bitwise operations to check if the bit has been set, and set the bit.\n\n# Complexity\n- Time complexity:\n$$O(n)$$ The worst case amount of iterations grows linearly with how long the input is.\n\n- Space complexity:\n$$O(1)$$ There will only be 26 possible letters (a-z) regardless of input, the space it takes up is constant.\n\n# Code\n```c\nchar repeatedCharacter(char* str) {\n int found = 0; // bit array\n while (*str) {\n int offset = 1 << (*str - \'a\');\n if (found & offset) return *str;\n found |= offset;\n ++str;\n }\n return 0;\n}\n``` | 2 | 0 | ['Bit Manipulation', 'C', 'Bitmask'] | 0 |
first-letter-to-appear-twice | C# solution using HashSet | c-solution-using-hashset-by-dmitriy-maks-22ki | Complexity\n- Time complexity: O(n)\n- Space complexity: O(n)\n\n# Code\n\npublic class Solution\n{\n public char RepeatedCharacter(string s)\n {\n | dmitriy-maksimov | NORMAL | 2023-09-22T08:42:59.817517+00:00 | 2023-09-22T08:42:59.817536+00:00 | 28 | false | # Complexity\n- Time complexity: $$O(n)$$\n- Space complexity: $$O(n)$$\n\n# Code\n```\npublic class Solution\n{\n public char RepeatedCharacter(string s)\n {\n var set = new HashSet<char>();\n\n foreach (var c in s)\n {\n if (set.Contains(c))\n {\n return c;\n }\n\n set.Add(c);\n }\n\n return \' \';\n }\n}\n``` | 2 | 0 | ['C#'] | 0 |
first-letter-to-appear-twice | [ Go Solution ]Great explanation and Full Description | go-solution-great-explanation-and-full-d-i37f | Intuition\nThis problem asks to find the first repeated character in a given string. We can solve this problem by scanning the string from left to right and kee | sansaian | NORMAL | 2023-07-27T09:33:42.081648+00:00 | 2023-07-27T09:33:42.081670+00:00 | 204 | false | # Intuition\nThis problem asks to find the first repeated character in a given string. We can solve this problem by scanning the string from left to right and keeping track of the characters we have seen before. As soon as we encounter a character that is already in our record, we return it as the first repeating character.\n\n# Approach\nThe approach in this implementation involves creating a map (set) to store characters in the string as we encounter them. We iterate over each character in the string, checking if the character is already in the set. If it is, we return it as the first repeated character. If it\'s not, we add it to the set. If we go through the entire string without finding a repeated character, we return a space character (\' \').\n\n# Complexity\n- Time complexity: The time complexity is O(n), where n is the length of the string. This is because we are iterating through each character in the string once.\n- Space complexity: The space complexity is also O(n), where n is the length of the string. This is because in the worst-case scenario, all characters in the string are unique, and we would need to store each of them in the set. However, since the set size won\'t exceed the unique characters in the ASCII table, we can also argue that the space complexity is O(1), or constant.\n\n# Code\n```\nfunc repeatedCharacter(s string) byte {\n set:=make(map[byte]struct{})\n for i:=0;i<len(s);i++{\n if _,ok:=set[s[i]];ok{\n return s[i]\n }\n\t\tset[s[i]]=struct{}{}\n }\n return \' \'\n}\n``` | 2 | 0 | ['Hash Table', 'String', 'Counting', 'Go'] | 0 |
first-letter-to-appear-twice | O(n) Solution for First Letter to Appear Twice Solution in C++ | on-solution-for-first-letter-to-appear-t-qltt | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | The_Kunal_Singh | NORMAL | 2023-05-27T04:36:36.819681+00:00 | 2023-05-27T04:36:36.819726+00:00 | 537 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n)\n# Code\n```\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n unordered_map<char, int> mp;\n int i;\n for(i=0 ; i<s.length() ; i++)\n {\n mp[s[i]]++;\n if(mp[s[i]]>1)\n return s[i];\n }\n return s[0];\n }\n};\n```\n\n | 2 | 0 | ['C++'] | 0 |
first-letter-to-appear-twice | Java || Beats 100% || O(1) Space, O(n) Time || Explanation | java-beats-100-o1-space-on-time-explanat-arhg | Intuition\nTo find the first letter that appears twice in the string, we can iterate over the characters in the string and keep track of which letters have been | Laziz_511 | NORMAL | 2023-05-20T09:50:56.520841+00:00 | 2023-05-20T09:50:56.520877+00:00 | 482 | false | # Intuition\nTo find the first letter that appears twice in the string, we can iterate over the characters in the string and keep track of which letters have been seen. Once we encounter a letter that has already been seen, we return it as the answer.\n\n# Approach\n1. We can use an array of booleans, **\'letters\'**, of size 26 to represent each letter of the alphabet. The index of each letter is calculated by subtracting **\'a\'** from the character value, as \'a\' has an **ASCII value of 97**.\n2. Initialize all elements of the **\'letters\'** array to **\'false\'**.\n3. Iterate through each character in the input string **\'s\'**.\n4. For each character, check if the corresponding element in the **\'letters\'** array is **\'true\'**. If it is, return the character as it is the first letter that appears twice.\n5. If the corresponding element is **\'false\'**, set it to **\'true\'** to mark that the letter has been seen.\n6. If no letter appears twice, return a space character **\' \'**.\n\n# Complexity\n- Time complexity:\n\nThe solution iterates through each character in the input string s, which takes **O(n)** time, where n is the length of the string. Accessing and updating the boolean array elements also takes constant time. Therefore, the overall time complexity is **O(n)**.\n\n\n- Space complexity:\n\nThe solution uses a boolean array of size 26 to represent each letter of the alphabet. Hence, the space complexity is O(26), which simplifies to **O(1)** as it is a **constant size**.\n\n# Code\n```\nclass Solution {\n public char repeatedCharacter(String s) {\n boolean[] letters = new boolean[26]; // Represents each letter of the alphabet\n \n for (int i = 0; i < s.length(); i++) {\n int index = s.charAt(i) - \'a\'; // Calculate the index of the current character\n \n if (letters[index]) {\n return s.charAt(i); // Found the first repeated letter\n } else {\n letters[index] = true; // Mark the letter as seen\n }\n }\n\n return \' \'; // No repeated letter found\n }\n}\n```\n\n- **"Please upvote if you found the solution helpful. Thank you!"**\n | 2 | 0 | ['Java'] | 1 |
first-letter-to-appear-twice | C++ || 0 ms || 100% Faster || | c-0-ms-100-faster-by-dil_da_ni_mada-ufb8 | Intuition\n Describe your first thoughts on how to solve this problem. \nStart iterating on the string and if element is not present on the set then insert it o | its_over | NORMAL | 2023-05-16T06:58:21.097867+00:00 | 2023-05-16T06:58:21.097908+00:00 | 67 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nStart iterating on the string and if element is not present on the set then insert it otherwise if already present then that the first repeating element just return the character.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nusing ordered set\n\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/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**\n* JAI SHREE RAM *\n**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/\n/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**\n* It\'s Not Over, Until i Win * \n**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n set<char> s1;\n for(auto & i: s) {\n if(s1.find(i) != s1.end()) {\n return i;\n }\n else {\n s1.insert(i);\n }\n }\n return \'0\';\n }\n};\n```\n\n\n | 2 | 0 | ['String', 'Ordered Set', 'C++'] | 0 |
first-letter-to-appear-twice | 5 line java code || Beats 100% || very easy beginner friendly || | 5-line-java-code-beats-100-very-easy-beg-xpbf | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | ammar_saquib | NORMAL | 2023-04-17T18:58:38.859931+00:00 | 2023-04-17T18:58:38.859984+00:00 | 297 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public char repeatedCharacter(String s) {\n Set<Character> ans=new HashSet();\n for(char c :s.toCharArray())\n {\n if(!ans.add(c))\n return c;\n }\n return \'a\';\n }\n}\n``` | 2 | 0 | ['Java'] | 0 |
first-letter-to-appear-twice | Easy Approach || 4 Lines Code || c++ | easy-approach-4-lines-code-c-by-tanveer_-tkgp | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | tanveer_965 | NORMAL | 2023-03-06T19:17:24.613026+00:00 | 2023-03-06T19:17:24.613058+00:00 | 657 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n unordered_map<char,int>mp;\n for(int i=0;i<s.size();i++)\n {\n mp[s[i]]++;\n if(mp[s[i]]==2) return s[i];\n }\n return -1;\n }\n};\n``` | 2 | 0 | ['C++'] | 0 |
first-letter-to-appear-twice | C++ Solution || Beats 100% || Runtime 0ms | c-solution-beats-100-runtime-0ms-by-asad-jv6r | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Asad_Sarwar | NORMAL | 2023-02-26T21:37:49.652574+00:00 | 2023-02-26T21:37:49.652622+00:00 | 2,046 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n*n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n\n int mini=INT_MAX;\n for(int i=0;i<s.size();i++)\n {\n for(int j=i+1;j<s.size();j++)\n {\n if(s[i]==s[j])\n {\n mini=min(mini,j);\n }\n }\n }\nreturn s[mini];\n\n }\n};\n``` | 2 | 0 | ['C++'] | 0 |
first-letter-to-appear-twice | 100% beat...... | 100-beat-by-dhruv_22_gupta-ofrf | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | dhruv_22_gupta | NORMAL | 2023-02-10T20:16:45.267098+00:00 | 2023-02-10T20:16:45.267134+00:00 | 516 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n int i=0;\n\n map<char,int> m;\n while(i!=s.size()){\n m[s[i]]++;\n if(m[s[i]]==2){\n return s[i];\n }\n i++;\n }\n return \'c\';\n }\n};\n``` | 2 | 0 | ['C++'] | 1 |
first-letter-to-appear-twice | 100% beats java solution for java beginners :-) | 100-beats-java-solution-for-java-beginne-palz | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nusing set in java\n\n# | krishnanshu_dwivedi | NORMAL | 2023-01-10T15:03:53.410188+00:00 | 2023-01-10T15:03:53.410222+00:00 | 449 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nusing set in java\n\n# Complexity\n- Time complexity:\n- O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO{1}\n\n# Code\n```\nclass Solution {\n public char repeatedCharacter(String s) {\n Set <Character> set = new HashSet<>();\n for(int i = 0 ; i<s.length(); i++){\n if(!set.add(s.charAt(i))) return s.charAt(i);\n }\n return \'a\';\n }\n}\n``` | 2 | 0 | ['Java'] | 1 |
first-letter-to-appear-twice | C++ | 0 ms | one liner | c-0-ms-one-liner-by-_kitish-m2oe | \nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n return [&,st=unordered_set<char>{}]()mutable{for(char &_s:s){if(st.count(_s)) retur | _kitish | NORMAL | 2022-12-01T06:59:35.430598+00:00 | 2022-12-01T06:59:35.430639+00:00 | 128 | false | ```\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n return [&,st=unordered_set<char>{}]()mutable{for(char &_s:s){if(st.count(_s)) return _s; st.insert(_s);} return \'$\';}();\n }\n};\n``` | 2 | 0 | [] | 0 |
first-letter-to-appear-twice | C++ solution 100% faster | c-solution-100-faster-by-om_limbhare-zj1o | \nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n vector <bool> v(26,false);\n for(char a:s){\n if(v[a-\'a\']) retur | om_limbhare | NORMAL | 2022-11-24T21:45:13.907095+00:00 | 2022-11-24T21:45:13.907128+00:00 | 982 | false | ```\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n vector <bool> v(26,false);\n for(char a:s){\n if(v[a-\'a\']) return a;\n v[a-\'a\']=1;\n }\n return \'a\'; \n }\n};\n\n\n``` | 2 | 0 | ['C', 'C++'] | 0 |
first-letter-to-appear-twice | JavaScript Object/Set | javascript-objectset-by-ian-of-yore-qdil | Intuition\nThis can be solved by using either object or set structure.\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n We can crea | ian-of-yore | NORMAL | 2022-11-19T21:25:14.403597+00:00 | 2022-11-19T21:25:14.403633+00:00 | 378 | false | # Intuition\nThis can be solved by using either object or set structure.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n We can create a new object/set. Then loop through the string, inside the loop, we first check if the letter has already be added to the object/set. If it is already in the object, then this is its second appearance. So, we simply return it. Otherwise, we add the letter to our object/set.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n)$$ as we loop through the string only once and loopup time complexity for object/set is a constant, $$O(n)$$ \n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n\n# Code\n```\n/**\n * @param {string} s\n * @return {character}\n */\nvar repeatedCharacter = function(s) {\n const map = {};\n\n for (let i = 0; i < s.length; i++) {\n if (map[s[i]]) {\n return s[i];\n }\n else {\n map[s[i]] = 1;\n }\n }\n};\n``` | 2 | 0 | ['JavaScript'] | 1 |
first-letter-to-appear-twice | Python || 6 Lines || 96.89% Faster || Using Dictionary || O(n) T.C. | python-6-lines-9689-faster-using-diction-331i | \nclass Solution:\n def repeatedCharacter(self, s: str) -> str:\n d={}\n for i in s:\n if i in d: \n d[i]+=1\n | pulkit_uppal | NORMAL | 2022-11-05T09:59:12.860691+00:00 | 2022-11-05T09:59:12.860734+00:00 | 354 | false | ```\nclass Solution:\n def repeatedCharacter(self, s: str) -> str:\n d={}\n for i in s:\n if i in d: \n d[i]+=1\n if d[i]==2: return i\n else: d[i]=1\n```\n\n**Please upvote if you like the solution** | 2 | 0 | ['Python'] | 0 |
first-letter-to-appear-twice | Python | python-by-akhilgullapalli-w0v2 | \nclass Solution(object):\n def repeatedCharacter(self, s):\n """\n :type s: str\n :rtype: str\n """\n twice = {}\n | akhilgullapalli | NORMAL | 2022-10-07T15:22:03.950293+00:00 | 2022-10-07T15:22:03.950328+00:00 | 522 | false | ```\nclass Solution(object):\n def repeatedCharacter(self, s):\n """\n :type s: str\n :rtype: str\n """\n twice = {}\n for i in s:\n if i in twice:\n return i\n else:\n twice[i] = 1\n``` | 2 | 0 | [] | 0 |
first-letter-to-appear-twice | Beats 99.86% [Python 3] | beats-9986-python-3-by-sneh713-rvpv | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Sneh713 | NORMAL | 2022-10-07T06:49:05.287534+00:00 | 2022-10-07T06:50:56.541384+00:00 | 994 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n- O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n- O(N=26) so O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def repeatedCharacter(self, s: str) -> str:\n dicta={}\n for i in s:\n if i in dicta:\n return i\n else:\n dicta[i]=1\n \n``` | 2 | 0 | ['Hash Table', 'Python3'] | 2 |
first-letter-to-appear-twice | Easy and short python solution | easy-and-short-python-solution-by-aastha-t88w | \nd={}\nfor i in range(len(s)):\n\tif s[i] not in d:\n\t\t\td[s[i]]=i\n\telse:\n\t\t\treturn s[i]\n | aastha1916 | NORMAL | 2022-10-06T09:28:57.223957+00:00 | 2022-10-06T09:28:57.223996+00:00 | 116 | false | ```\nd={}\nfor i in range(len(s)):\n\tif s[i] not in d:\n\t\t\td[s[i]]=i\n\telse:\n\t\t\treturn s[i]\n``` | 2 | 0 | ['Python'] | 0 |
first-letter-to-appear-twice | C++|100% Faster|0 ms runtime | c100-faster0-ms-runtime-by-infinix_05-v7ak | UPVOTE AND COMMENT YOUR DOUBTS\n\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n int i;\n for(i=0;i<s.length();i++){\n | infinix_05 | NORMAL | 2022-09-10T13:59:53.539184+00:00 | 2022-09-10T13:59:53.539230+00:00 | 96 | false | UPVOTE AND COMMENT YOUR DOUBTS\n```\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n int i;\n for(i=0;i<s.length();i++){\n for(int j=0;j<i;j++){\n if(s[i]==s[j]){\n return s[i];\n break;\n }\n }\n }\n \n return s[i];\n }\n};\n```\nUPVOTE AND COMMENT YOUR DOUBTS | 2 | 0 | ['C'] | 0 |
first-letter-to-appear-twice | 100% FASTER || HASHMAP || SIMPLE JAVA CODE | 100-faster-hashmap-simple-java-code-by-p-nzmk | \n HashMap<Character,Integer> map= new HashMap<>();\n for(int i=0;i<s.length();i++){\n char c= s.charAt(i);\n if(map.containsKe | priyankan_23 | NORMAL | 2022-08-31T14:48:55.234217+00:00 | 2022-08-31T14:48:55.234247+00:00 | 101 | false | ```\n HashMap<Character,Integer> map= new HashMap<>();\n for(int i=0;i<s.length();i++){\n char c= s.charAt(i);\n if(map.containsKey(c)){\n map.put(c,map.get(c)+1);\n if(map.get(c)==2) return c;\n break;\n \n }\n else\n map.put(c,1);\n }\n \n \n return 0;\n \n }\n}\n``` | 2 | 0 | [] | 0 |
first-letter-to-appear-twice | Bit Masking | bit-masking-by-shahan989-krsp | python\nclass Solution:\n def repeatedCharacter(self, s: str) -> str:\n \n #bit masking\n \n value = 0\n \n for i i | shahan989 | NORMAL | 2022-08-31T12:08:42.545810+00:00 | 2022-08-31T12:08:42.545852+00:00 | 45 | false | ```python\nclass Solution:\n def repeatedCharacter(self, s: str) -> str:\n \n #bit masking\n \n value = 0\n \n for i in range(len(s)):\n \n char = s[i]\n \n if value & (1 <<ord( char) - ord("a")):\n return char\n value |= 1 << ord(char) - ord("a")\n``` | 2 | 0 | ['Bitmask', 'Python'] | 0 |
first-letter-to-appear-twice | bit manipulation c solution | bit-manipulation-c-solution-by-nameldi-5y4h | \nchar repeatedCharacter(char * s){\n int len = strlen(s);\n int bitmask = 0;\n for(int i = 0; i < len; i++){\n if(bitmask & (1 << (s[i]-\'a\')) | nameldi | NORMAL | 2022-08-19T13:24:05.246189+00:00 | 2022-08-19T13:24:05.246239+00:00 | 88 | false | ```\nchar repeatedCharacter(char * s){\n int len = strlen(s);\n int bitmask = 0;\n for(int i = 0; i < len; i++){\n if(bitmask & (1 << (s[i]-\'a\')))\n return s[i];\n bitmask |= (1 << (s[i]-\'a\'));\n }\n return -1;\n}\n``` | 2 | 0 | ['C'] | 0 |
first-letter-to-appear-twice | Java ,0ms Solution, array only | java-0ms-solution-array-only-by-banurr-j9fh | \npublic static char firstUniqChar(String s)\n {\n int[] aa = new int[26];\n for(Character i : s.toCharArray())\n | banurr | NORMAL | 2022-08-17T06:55:56.522601+00:00 | 2022-08-17T06:55:56.522637+00:00 | 164 | false | ```\npublic static char firstUniqChar(String s)\n {\n int[] aa = new int[26];\n for(Character i : s.toCharArray())\n {\n ++aa[i-\'a\'];\n if(aa[i-\'a\']==2) return i;\n }\n return 0;\n }\n``` | 2 | 0 | ['Array', 'Java'] | 0 |
first-letter-to-appear-twice | ✅C++ || Vesy Easy || Map || O(n) | c-vesy-easy-map-on-by-tridibdalui04-bcku | O(n) Solution using map explanation in Comment\n\nApproch :-\n Traverse the whole String from the start and store the charecter & it\'s freqency in a map one by | tridibdalui04 | NORMAL | 2022-08-16T14:18:32.930690+00:00 | 2022-08-16T14:18:32.930728+00:00 | 190 | false | **O(n) Solution using map explanation in Comment**\n\n**Approch :-**\n* Traverse the whole *String* from the start and store the charecter & it\'s freqency in a map one by one\n\n* Travese the map and if there is a charecter with frequency return the current charecter as it is the first what occures 2 times\n\n# Code\n\n```\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n unordered_map<char,int>mp;\n for(int i=0;i<s.size();i++)\n {\n mp[s[i]]++; // store the charecters and its frequency\n for(auto i:mp) // traverse the map within\n {\n if(i.second==2) // if there\'s any charecter with frequency 2\n return i.first; //return the charecter\n }\n }\n return \' \';//if there\'s no then return empty charecter\n }\n};\n``` | 2 | 0 | ['C'] | 1 |
first-letter-to-appear-twice | Java using Set | java-using-set-by-carolinewillis211-yjqx | ```class Solution {\n public char repeatedCharacter(String s) {\n Set set = new HashSet<>();\n \n for(int i = 0; i < s.length(); i++) { | carolinewillis211 | NORMAL | 2022-08-16T00:47:22.154878+00:00 | 2022-08-16T00:47:22.154919+00:00 | 79 | false | ```class Solution {\n public char repeatedCharacter(String s) {\n Set <Character> set = new HashSet<>();\n \n for(int i = 0; i < s.length(); i++) {\n char curr = s.charAt(i);\n if(set.contains(curr)) {\n return curr;\n }\n \n set.add(curr);\n }\n \n return \'v\'; //will never reach here\n }\n} | 2 | 0 | [] | 0 |
first-letter-to-appear-twice | C++ || 0 ms || 0(n) Time Complexity || Using Visited Array | c-0-ms-0n-time-complexity-using-visited-vmowl | My approach here was to iterate through the string and increase the count of current character in visitedAlphabets array/vector. If we get the count of any char | mohan792 | NORMAL | 2022-08-08T11:42:25.514563+00:00 | 2022-08-08T11:42:25.514588+00:00 | 30 | false | My approach here was to iterate through the string and increase the count of current character in visitedAlphabets array/vector. If we get the count of any character as 2, it will be the required output.\n\n```\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n vector<int> visitedAlphabets(26, 0);\n int len = s.size();\n \n for(int i = 0; i < len; i++){\n visitedAlphabets[s[i] - \'a\']++;\n \n if(visitedAlphabets[s[i] - \'a\'] == 2)\n return s[i];\n }\n \n return \' \';\n }\n};\n```\n\nPlease upvote if you llike the solution... ;) | 2 | 0 | ['C', 'C++'] | 0 |
first-letter-to-appear-twice | Solution Using HashSet | solution-using-hashset-by-bharg4vi-747q | ```\nclass Solution {\n public char repeatedCharacter(String s)\n {\n\n HashSet m = new HashSet();\n for(int i = 0; i < s.length(); i++)\n | bharg4vi | NORMAL | 2022-08-05T13:55:54.384976+00:00 | 2022-08-05T13:55:54.385025+00:00 | 127 | false | ```\nclass Solution {\n public char repeatedCharacter(String s)\n {\n\n HashSet<Character> m = new HashSet<Character>();\n for(int i = 0; i < s.length(); i++)\n {\n if(!m.contains(s.charAt(i)))\n {\n m.add(s.charAt(i));\n }\n else\n {\n return s.charAt(i);\n }\n\n\n }\n\n return \'a\';\n\n }\n}\n | 2 | 0 | ['Java'] | 1 |
first-letter-to-appear-twice | [Go] Bitmask | go-bitmask-by-sebnyberg-b5dd | Since there are only 26 lowercase letters in the alphabet, \nwe can use a 32-bit integer to track whether a letter has been seen before.\n\nThe first round, the | sebnyberg | NORMAL | 2022-08-05T10:10:22.008693+00:00 | 2022-08-05T10:10:51.858664+00:00 | 82 | false | Since there are only 26 lowercase letters in the alphabet, \nwe can use a 32-bit integer to track whether a letter has been seen before.\n\nThe first round, the corresponding bit is set. The second round, we return the result.\n\n```go\nfunc repeatedCharacter(s string) byte {\n\t// Use a bitmask to keep track of whether a character has\n\t// been seen before.\n\tvar seen int\n\tfor _, ch := range s {\n\t\tbit := (1 << (ch - \'a\'))\n\t\tif seen&bit > 0 {\n\t\t\treturn byte(ch)\n\t\t}\n\t\tseen |= bit\n\t}\n\treturn 0\n}\n``` | 2 | 0 | ['Bit Manipulation', 'Go'] | 0 |
first-letter-to-appear-twice | C++ || Easy solution | c-easy-solution-by-shuvam_barman-3lo4 | \nclass Solution {\npublic:\n array<int, 26> counter;\n char repeatedCharacter(string s) {\n for (const auto i : s)\n if (counter[i - \' | Shuvam_Barman | NORMAL | 2022-08-03T15:44:10.720170+00:00 | 2022-08-03T15:44:10.720213+00:00 | 121 | false | ```\nclass Solution {\npublic:\n array<int, 26> counter;\n char repeatedCharacter(string s) {\n for (const auto i : s)\n if (counter[i - \'a\']++) return i;\n throw \'a\';\n }\n};\n``` | 2 | 0 | ['C'] | 0 |
first-letter-to-appear-twice | Easy C++ solution || Using set || chill life ✅ | easy-c-solution-using-set-chill-life-by-a2q18 | Idea is traverse the string, if you find the character is set return it if not then insert it. Simple\n\nchar repeatedCharacter(string s) {\n set<char>st | Soumik_Coder | NORMAL | 2022-07-28T19:46:34.218262+00:00 | 2022-07-28T19:46:34.218327+00:00 | 68 | false | Idea is traverse the string, if you find the character is set return it if not then insert it. Simple\n```\nchar repeatedCharacter(string s) {\n set<char>st;\n int i=0;\n for(int i=0; i<s.length(); i++)\n {\n if(st.count(s[i])){\n return s[i];\n }\n st.insert(s[i]);\n }\n return \'a\';\n }\n\t```\n\tIf it helps you, please upvote me | 2 | 0 | ['C', 'Ordered Set'] | 1 |
first-letter-to-appear-twice | C++ || 2 easiest solutions | c-2-easiest-solutions-by-girdharnishant-ik8c | First, solving this problem using multiset.\nMultiset is a data structure which stores value and can easily return the count of an element present in the multis | girdharnishant | NORMAL | 2022-07-28T19:23:08.834846+00:00 | 2022-07-28T19:23:08.834877+00:00 | 176 | false | First, solving this problem using multiset.\nMultiset is a data structure which stores value and can easily return the count of an element present in the multiset.\n\n\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n \n multiset<char> ms; // Creating a multiset.\n for(int i = 0; i < s.size(); i++){\n ms.insert(s[i]); // Inserting element in the multiset.\n if(ms.count(s[i]) == 2) return s[i]; // Checking count of the element.\n }\n return \'a\'; // Just returning any random that will definetely never execute.\n }\n};\n\nSecond, solving it using an array of all alphabets, we\'ll create an array of size 26 initializing it with 0. We\'ll iterate over the given string and increment the index of a specific alphabet and then check if after the increment it is equal to 2, if yes return that character else continue iterating.\n\n\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n \n int arr[26] = {}; // Array created and initialized by 0.\n for(int i = 0; i < s.size(); i++){\n arr[s[i] - \'a\']++; // Incrementing the index of a specidied alphabet.\n if(arr[s[i] - \'a\'] == 2) return s[i]; // Checking if the index has a value of 2\n }\n return \'a\'; // Just returning any random that will definetely never execute.\n }\n};\n | 2 | 0 | ['C', 'C++'] | 1 |
first-letter-to-appear-twice | CPP | O(n) Solution | Easy to understand | cpp-on-solution-easy-to-understand-by-pr-6w3w | class Solution {\npublic:\n char repeatedCharacter(string s) {\n mapmp1;\n for(char c=\'a\';c<=\'z\';c++){\n mp1[c]++;\n mp1[c | prince_7672526 | NORMAL | 2022-07-26T23:14:15.052762+00:00 | 2022-07-27T05:58:34.796292+00:00 | 41 | false | class Solution {\npublic:\n char repeatedCharacter(string s) {\n map<char,int>mp1;\n for(char c=\'a\';c<=\'z\';c++){\n mp1[c]++;\n mp1[c]++;\n }\n for (int i=0;i<s.size();i++){\n mp1[s[i]]--;\n if(mp1[s[i]]==0){\n return s[i];\n }\n }\n return 0;\n }\n}; | 2 | 0 | [] | 2 |
first-letter-to-appear-twice | Easy O(n) solution in Python using set | easy-on-solution-in-python-using-set-by-w7nkb | We create a null/empty set. \nNext, we traverse the string character by character and if the character is not present in set, we add that character in the seen | indranilbhattacharya9874273176 | NORMAL | 2022-07-26T10:20:33.006449+00:00 | 2022-07-26T12:08:17.210551+00:00 | 116 | false | We create a null/empty set. \nNext, we traverse the string character by character and if the character is not present in set, we add that character in the seen set, otherwise the character is in the seen set. Means we can return that character as it is appearing twice.\nNote that the search in a set in Python is O(1) operation.\n\n\n```\nclass Solution(object):\n def repeatedCharacter(self, s):\n """\n :type s: str\n :rtype: str\n """\n \n seen = set()\n for i in s:\n if i in seen:\n return i\n else:\n seen.add(i)\n\t\t\t\t\n``` | 2 | 0 | ['Ordered Set', 'Python'] | 0 |
first-letter-to-appear-twice | Easy CPP solution | easy-cpp-solution-by-reehan9-ais3 | \nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n int i = 0;\n unordered_map<char , int>check;\n for(i = 0 ; i < s.size | Reehan9 | NORMAL | 2022-07-24T12:34:39.603528+00:00 | 2022-07-24T16:31:18.449303+00:00 | 69 | false | ```\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n int i = 0;\n unordered_map<char , int>check;\n for(i = 0 ; i < s.size() && ++check[s[i]]<=2; i++ , check[s[i]]++);\n return s[i];\n }\n};\n```\n##### Please upvote if you find this helpful\nhttps://github.com/Reehan9/Leetcode-Solutions\n\n | 2 | 0 | ['C++'] | 1 |
first-letter-to-appear-twice | Swift - Simple solution | swift-simple-solution-by-evkobak-7p2p | \nclass Solution {\n func repeatedCharacter(_ s: String) -> Character {\n \n var set: Set<Character> = []\n\n for char in s {\n | evkobak | NORMAL | 2022-07-24T10:32:41.324533+00:00 | 2022-07-24T10:32:41.324559+00:00 | 135 | false | ```\nclass Solution {\n func repeatedCharacter(_ s: String) -> Character {\n \n var set: Set<Character> = []\n\n for char in s {\n if set.contains(char) {\n return char\n } else {\n set.insert(char)\n }\n }\n\n return "a"\n }\n}\n``` | 2 | 0 | ['Swift'] | 1 |
first-letter-to-appear-twice | C++ | Hashing | Easy | c-hashing-easy-by-gamistl0314-7nby | Time Complexity - O(N)\nSpace Complexity - O(26)\n\nchar repeatedCharacter(string s) \n {\n unordered_map<char,int> m;\n int n=s.length();\n | gamistl0314 | NORMAL | 2022-07-24T08:36:11.012876+00:00 | 2022-07-24T08:36:11.012914+00:00 | 20 | false | Time Complexity - **O(N)**\nSpace Complexity - **O(26)**\n```\nchar repeatedCharacter(string s) \n {\n unordered_map<char,int> m;\n int n=s.length();\n char c;\n for(int i=0;i<n;i++)\n {\n if(!m[s[i]]) m[s[i]]++;\n else\n {\n c=s[i];\n break;\n }\n }\n return c;\n }\n``` | 2 | 0 | ['C'] | 0 |
first-letter-to-appear-twice | simple || like two sum || o(n) | simple-like-two-sum-on-by-mamtachahal-5h7d | ```\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n unordered_mapm;\n char ans;\n for(int i=0;i<s.size();i++){\n | Mamtachahal | NORMAL | 2022-07-24T06:26:20.355249+00:00 | 2022-07-24T06:26:20.355290+00:00 | 188 | false | ```\nclass Solution {\npublic:\n char repeatedCharacter(string s) {\n unordered_map<char,int>m;\n char ans;\n for(int i=0;i<s.size();i++){\n \n if(m.find(s[i])!=m.end())\n {\n ans= s[i];\n break;\n }\n m[s[i]]++;\n \n \n }\n return ans;\n }\n}; | 2 | 0 | ['C', 'C++'] | 1 |
make-a-square-with-the-same-color | Visualized || Brute Force + General Solution || C++ | visualized-brute-force-general-solution-qlhf3 | 1. Brute Force:\n# Intuition \n- Only four 2x2 Squares are possible, check for all of them.\n# Approach \n1. The problem requires determining if it\'s possible | fahad_Mubeen | NORMAL | 2024-04-27T16:03:03.106042+00:00 | 2024-05-06T03:35:01.299726+00:00 | 2,046 | false | ## 1. Brute Force:\n# Intuition \n- Only four **2x2 Squares** are possible, check for all of them.\n# Approach \n1. The problem requires determining if it\'s possible to create a 2x2 square of the same color by changing at most one cell in the given 3x3 grid. \n2. Thus we examines each potential 2x2 square configuration around the central cell (grid[1][1]) in the 3x3 grid, incrementing counts for \'W\' and \'B\' cells. \n3. After counting the \'W\' and \'B\' cells in each potential square, verify if either \'W\' or \'B\' count exceeds 2. If either count exceeds 2 in any of the squares, it\'s possible to create a 2x2 square of the same color. In such case we return True.\n4. If no such square is found we return false.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Visual Representation \n\n\n\n# Complexity\n- Time complexity: O(1)\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n bool canMakeSquare(vector<vector<char>>& grid) {\n char ch = grid[1][1];\n int W = 0, B = 0;\n ch == \'W\' ? W++ : B++;\n // first box\n (grid[0][0] == \'W\' ? W++ : B++);\n (grid[0][1] == \'W\' ? W++ : B++);\n (grid[1][0] == \'W\' ? W++ : B++);\n if(W > 2 || B > 2) return 1;\n W = 0, B = 0; ch == \'W\' ? W++ : B++;\n\n // second box\n (grid[0][1] == \'W\' ? W++ : B++);\n (grid[0][2] == \'W\' ? W++ : B++);\n (grid[1][2] == \'W\' ? W++ : B++);\n if(W > 2 || B > 2) return 1;\n W = 0, B = 0; ch == \'W\' ? W++ : B++;\n\n // third box\n (grid[1][0] == \'W\' ? W++ : B++);\n (grid[2][0] == \'W\' ? W++ : B++);\n (grid[2][1] == \'W\' ? W++ : B++);\n if(W > 2 || B > 2) return 1;\n W = 0, B = 0; ch == \'W\' ? W++ : B++;\n\n // fourth box\n (grid[1][2] == \'W\' ? W++ : B++);\n (grid[2][1] == \'W\' ? W++ : B++);\n (grid[2][2] == \'W\' ? W++ : B++);\n if(W > 2 || B > 2)return 1;\n \n return 0;\n }\n};\n```\n## 2. General Solution\nIf the input matrix is 4x4 or larger, implementing brute force becomes tedious and often impractical. For larger matrices, a general solution applicable to all sizes becomes necessary. I have provided the code for the same below.\n# Complexity \n1. Time Complexity: O(*(size of matrix* - *size of square)*) almost **O(n)** for *square of size 2 and matrix of size n.*\n2. Space Complexity: O(1);\n\n```\nclass Solution {\npublic:\n bool isPossible(vector<vector<char>>& grid, int i, int j){\n int W = 0, B = 0;\n for(int x = i; x < i + 2; x++){\n for(int y = j; y < j + 2; y++){\n if(grid[x][y] == \'W\') W++;\n else B++;\n }\n }\n if(W > 2 || B > 2) return 1;\n return 0;\n }\n bool canMakeSquare(vector<vector<char>>& grid){\n int n = grid.size();\n for(int i = 0; i <= n - 2; i++){ // n- 2 since square size is 2\n for(int j = 0; j <= n - 2; j++){\n if(isPossible(grid, i, j)) return 1;\n }\n }\n return 0;\n }\n};\n``` | 20 | 1 | ['Math', 'Matrix', 'C++'] | 7 |
make-a-square-with-the-same-color | Python 3 || 4 lines, countOf || T/S: 99% / 23% | python-3-4-lines-countof-ts-99-23-by-spa-4tuu | Here\'s the intuition:\nThe problem is equvalent to determining whether any of the four 2x2 squares in grid do NOT have exactly 2 black (or white) squares.\n\nH | Spaulding_ | NORMAL | 2024-04-27T16:04:10.599980+00:00 | 2024-05-24T17:59:30.524560+00:00 | 470 | false | Here\'s the intuition:\nThe problem is equvalent to determining whether any of the four 2x2 squares in `grid` do NOT have exactly 2 black (or white) squares.\n\nHere\'s the plan:\nWe iterate through each of the four 2x2 squares, determining whether its count of black cells is NOT two. If we find one such square, we return `True`. If not, we return `False`.\n\n```\nclass Solution:\n def canMakeSquare(self, grid: List[List[str]]) -> bool:\n\n for i,j in product([0,1],[0,1]):\n square =(grid[i ][j ] + grid[i ][j+1] +\n grid[i+1][j ] + grid[i+1][j+1])\n\n if countOf(square, "B") != 2: return True\n \n return False\n```\n[https://leetcode.com/problems/make-a-square-with-the-same-color/submissions/1243409632/](https://leetcode.com/problems/make-a-square-with-the-same-color/submissions/1243409632/)\n\nI could be wrong, but I think that time complexity is *O*(1) and space complexity is *O*(1) because the matrix dimensions are 3 x 3. | 14 | 0 | ['Python', 'Python3'] | 0 |
make-a-square-with-the-same-color | Easy Video Solution 🔥 || Interview Approach || Brute Force Solution | easy-video-solution-interview-approach-b-vyzd | If you like the solution Please Upvote and subscribe to my youtube channel\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \nDry Run | ayushnemmaniwar12 | NORMAL | 2024-04-27T16:02:42.578469+00:00 | 2024-04-27T17:12:58.581468+00:00 | 1,212 | false | ***If you like the solution Please Upvote and subscribe to my youtube channel***\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nDry Run few examples and note your observations clearly\n\n\n\n***Easy Video Explanation***\n\nhttps://youtu.be/wo-tlC_Ms1s\n\n# Code\n```\nclass Solution {\npublic:\n bool canMakeSquare(vector<vector<char>>& v) {\n for(int i=0;i<2;i++) {\n int w=0,b=0;\n for(int j=0;j<2;j++) {\n if(v[i][j]==\'W\')\n w++;\n else\n b++;\n if(v[i+1][j]==\'W\')\n w++;\n else\n b++;\n }\n if(w>=3 || b>=3)\n return true;\n w=0,b=0;\n for(int j=1;j<3;j++) {\n if(v[i][j]==\'W\')\n w++;\n else\n b++;\n if(v[i+1][j]==\'W\')\n w++;\n else\n b++;\n }\n if(w>=3 || b>=3)\n return true;\n \n }\n return false;\n }\n};\n``` | 14 | 2 | ['Matrix', 'C++'] | 2 |
make-a-square-with-the-same-color | Easy solution - 27/04/2024 contest | easy-solution-27042024-contest-by-mangeu-2jwj | Intuition\n Describe your first thoughts on how to solve this problem. \nWe just check for the 4 top left square if there is at least 3 square with the same col | MangeurDeSushi | NORMAL | 2024-04-27T16:02:18.035742+00:00 | 2024-04-27T17:19:59.789598+00:00 | 1,400 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe just check for the 4 top left square if there is at least 3 square with the same color under, on the right, and under on the right, if that\'s the case, we can just modify one of this square to have 4 squares with the same color.\ndon\'t hesitate to upvote if you like the solution\n\n```python3 []\nclass Solution:\n def canMakeSquare(self, grid: List[List[str]]) -> bool:\n def change(x, y):\n colors = [grid[x][y], grid[x][y+1], grid[x+1][y], grid[x+1][y+1]]\n count = {\'B\': 0, \'W\': 0}\n for c in colors:\n count[c] += 1\n return count[\'B\'] >= 3 or count[\'W\'] >= 3\n\n for i in range(2):\n for j in range(2):\n if change(i, j):\n return True\n return False\n```\n``` cpp []\nclass Solution {\npublic:\n bool canMakeSquare(vector<vector<char>>& grid) {\n return change(grid, 0, 0) || change(grid, 0, 1) \n || change(grid, 1, 0) || change(grid, 1, 1);\n }\n\nprivate:\n bool change(vector<vector<char>>& grid, int x, int y) {\n char colors[4] = {grid[x][y], grid[x][y + 1], grid[x + 1][y], grid[x + 1][y + 1]};\n int countB = 0;\n int countW = 0;\n for (char c : colors) {\n if (c == \'B\')\n countB++;\n else\n countW++;\n }\n return countB >= 3 || countW >= 3;\n }\n};\n```\n```java []\nclass Solution {\n public boolean canMakeSquare(char[][] grid) {\n return change(grid, 0, 0) || change(grid, 0, 1) \n || change(grid, 1, 0) || change(grid, 1, 1);\n }\n\n private boolean change(char[][] grid, int x, int y) {\n char[] colors = {grid[x][y], grid[x][y + 1], grid[x + 1][y], grid[x + 1][y + 1]};\n int countB = 0;\n int countW = 0;\n for (char c : colors) {\n if (c == \'B\')\n countB++;\n else\n countW++;\n }\n return countB >= 3 || countW >= 3;\n }\n}\n```\n\n | 14 | 0 | ['Array', 'Math', 'Matrix', 'Python', 'C++', 'Java', 'Python3'] | 2 |
make-a-square-with-the-same-color | Simple java solution | simple-java-solution-by-siddhant_1602-aex2 | Complexity\n- Time complexity: O(1)\n\n- Space complexity: O(1)\n\n# Code\n\nclass Solution {\n public boolean canMakeSquare(char[][] grid) {\n for(in | Siddhant_1602 | NORMAL | 2024-04-27T16:01:36.609005+00:00 | 2024-04-27T16:01:36.609027+00:00 | 949 | false | # Complexity\n- Time complexity: $$O(1)$$\n\n- Space complexity: $$O(1)$$\n\n# Code\n```\nclass Solution {\n public boolean canMakeSquare(char[][] grid) {\n for(int i=0;i<=1;i++)\n {\n for(int j=0;j<=1;j++)\n {\n if(check(grid,i,j))\n {\n return true;\n }\n }\n }\n return false;\n }\n private boolean check(char grid[][], int i, int j)\n {\n int count=0,count1=0;\n for(int i1=i;i1<i+2;i1++)\n {\n for(int j1=j;j1<j+2;j1++)\n {\n if(grid[i1][j1] == \'B\')\n {\n count++;\n }\n else\n {\n count1++;\n }\n }\n }\n return count<=1 || count1<=1;\n }\n}\n``` | 13 | 0 | ['Java'] | 2 |
make-a-square-with-the-same-color | Easy Solution - Just Count White and Black Color. | easy-solution-just-count-white-and-black-9suu | Intuition\n- Count the number of white and black cell in each (2 x 2) matrix.\n Describe your first thoughts on how to solve this problem. \n\n# Complexity\n- T | ANMOL_IS_HERE | NORMAL | 2024-04-27T18:22:54.429291+00:00 | 2024-04-27T18:32:05.728821+00:00 | 201 | false | # Intuition\n- Count the number of white and black cell in each (2 x 2) matrix.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Complexity\n- Time complexity:O(2*2) Since size of matrix is definate (given in Constraints).\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1) i.e constant space.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n// \uD83D\uDE07 Please upvoat if you like the Solution. \uD83D\uDE07\n\nclass Solution {\npublic:\n bool canMakeSquare(vector<vector<char>>& grid) {\n \n for(int i = 0; i < 2; i++){\n for(int j = 0; j < 2; j++){\n int w = 0, b = 0;\n if(grid[i][j] == \'B\') b++;\n else w++;\n \n if(grid[i][j+1] == \'B\') b++;\n else w++;\n \n if(grid[i+1][j+1] == \'B\') b++;\n else w++;\n \n if(grid[i+1][j] == \'B\') b++;\n else w++;\n \n if(w >=3 or b >=3) return true;\n }\n }\n return false;\n }\n};\n``` | 9 | 0 | ['Counting', 'C++'] | 0 |
make-a-square-with-the-same-color | C++ || Pre-Computation 2D Sum | c-pre-computation-2d-sum-by-abhay5349sin-zva6 | Connect with me on LinkedIn: https://www.linkedin.com/in/abhay5349singh/\n\nApproach:\n mapping white:0 and blue:1\n pre-computing sum matrix for rectangle [x1, | abhay5349singh | NORMAL | 2024-04-27T16:01:54.310877+00:00 | 2024-04-27T17:21:13.889423+00:00 | 873 | false | **Connect with me on LinkedIn**: https://www.linkedin.com/in/abhay5349singh/\n\n**Approach:**\n* mapping `white:0` and `blue:1`\n* pre-computing sum matrix for rectangle `[x1, y2] => [x2, y2]`\n* consider square window of size `k` (here it\'s 2) and checking bounded sum\n\t* `sum = 1` (change 1 blue to white), `sum = 3` (change 1 white to blue)\n\t* `sum = 0` (all whites), `sum = 4` (all blues)\n\n```\nclass Solution {\npublic:\n bool canMakeSquare(vector<vector<char>>& grid) {\n int n=grid.size(), m=grid[0].size();\n \n vector<vector<int>> preSum(n,vector<int>(m,0));\n \n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n preSum[i][j] = (grid[i][j] == \'W\' ? 0:1);\n \n if(i>0) preSum[i][j] += preSum[i-1][j];\n if(j>0) preSum[i][j] += preSum[i][j-1];\n if(i>0 && j>0) preSum[i][j] -= preSum[i-1][j-1];\n }\n }\n \n int k = 2; // square window size\n \n for(int i=0;i<n-(k-1);i++){\n for(int j=0;j<m-(k-1);j++){\n int r1=i, c1=j, r2=i+1, c2=j+1;\n \n int sum = preSum[r2][c2];\n if(r1>0) sum -= preSum[r1-1][c2];\n if(c1>0) sum -= preSum[r2][c1-1];\n if(r1>0 && c1>0) sum += preSum[r1-1][c1-1];\n \n if(sum == 1 || sum == 3 || sum == 0 || sum == 4) return true;\n }\n }\n \n return false;\n }\n};\n```\n\n**Do upvote if it helps :)** | 8 | 1 | [] | 1 |
make-a-square-with-the-same-color | [Python3] Simple Solution | python3-simple-solution-by-dolong2110-vsp1 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | dolong2110 | NORMAL | 2024-04-28T07:52:38.383708+00:00 | 2024-04-28T07:52:38.383738+00:00 | 92 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(2 * 2)$$\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```\nclass Solution:\n def canMakeSquare(self, grid: List[List[str]]) -> bool:\n for i in range(2):\n for j in range(1, 3):\n cnt_w = 0\n for di, dj in [(0, 0), (0, -1), (1, 0), (1, -1)]:\n if grid[i + di][j + dj] == "W": cnt_w += 1\n if cnt_w != 2: return True\n return False\n``` | 5 | 0 | ['Matrix', 'Python3'] | 0 |
make-a-square-with-the-same-color | check 4 2x2 squares||0ms beats 100% | check-4-2x2-squares0ms-beats-100-by-anwe-ianz | Intuition\n Describe your first thoughts on how to solve this problem. \nThere are 4 2x2 squares.\nCheck every one\n# Approach\n Describe your approach to solvi | anwendeng | NORMAL | 2024-04-27T16:25:25.140626+00:00 | 2024-04-27T16:25:25.140648+00:00 | 170 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThere are 4 2x2 squares.\nCheck every one\n# Approach\n<!-- Describe your approach to solving the problem. -->\nIt needs to check any of the square has number of \'B\' <=1 or >=3 then return true. If none return false\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n# Code\n```\nclass Solution {\npublic:\n bool canMakeSquare(vector<vector<char>>& g) {\n int L[4];\n L[0]=(g[0][0]==\'B\')+(g[0][1]==\'B\')+(g[1][0]==\'B\')+(g[1][1]==\'B\');\n L[1]=(g[0][1]==\'B\')+(g[0][2]==\'B\')+(g[1][1]==\'B\')+(g[1][2]==\'B\');\n L[2]=(g[1][0]==\'B\')+(g[1][1]==\'B\')+(g[2][0]==\'B\')+(g[2][1]==\'B\');\n L[3]=(g[1][1]==\'B\')+(g[1][2]==\'B\')+(g[2][1]==\'B\')+(g[2][2]==\'B\');\n for(int i=0; i<4; i++){\n if (L[i]<=1 || L[i]>=3) return 1;\n }\n return 0;\n }\n};\n\n``` | 5 | 1 | ['C++'] | 1 |
make-a-square-with-the-same-color | simplest approach | two loops | count | simplest-approach-two-loops-count-by-use-8gnh | Intuition\n Describe your first thoughts on how to solve this problem. \nThe intution is to find what the neighbours of the current cell is doing.\nhere neighbo | user8093wK | NORMAL | 2024-04-28T08:23:47.923657+00:00 | 2024-04-28T08:23:47.923683+00:00 | 104 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intution is to find what the neighbours of the current cell is doing.\nhere neighbours of grid[ i ][ j ] is\n grid[ i ] [ j+1 ],\n grid[ i+1 ] [ j ],\n grid[ i+1 ] [ j+1 ].\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nSo, we check that is neighbours are same as the current cell or not(either B or W). we keep counting neighbour\'s matching frequecy.\n\nthe can be 2 cases to find our desired 2x2 subgrid of same color:\n\n1. **if atleast 2 neighbours are of same color**: then remaining 1 can be changed and we get our desired 2x2 subgrid\n2. **if none of the neighbour are of same color**: then we will change color of the current cell itself to get our desired 2x2 subgrid\n\nNOTE: do not run loop for 3x3 times here, it will get out bound then.\nwe can check upto 2 rows and columns and still get the desired output \n\n# Complexity\n- Time complexity: O(2^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n\n# Code\n```\nclass Solution {\npublic:\n bool canMakeSquare(vector<vector<char>>& grid) {\n for (int i = 0; i < 2; i++) {\n for (int j = 0; j < 2; j++) {\n int score = 0;\n if (grid[i][j] == grid[i][j + 1])\n score++;\n if (grid[i][j] == grid[i + 1][j])\n score++;\n if (grid[i][j] == grid[i + 1][j + 1])\n score++;\n if (score != 1) {\n return true;\n }\n }\n }\n return false;\n }\n};\n``` | 4 | 0 | ['C++'] | 0 |
make-a-square-with-the-same-color | 💥☠️🔥 Easiest C++✅Python3✅C#✅Java✅Python🐍✅C✅👾 Explained🏆 Contest - 27/04/2024 ⚡💥🔥✨⭐ | easiest-cpython3cjavapythonc-explained-c-ugja | Intuition\n\n\n\n\n\n Describe your first thoughts on how to solve this problem. \nC++ []\nclass Solution {\npublic:\n bool canMakeSquare(vector<vector<char> | Edwards310 | NORMAL | 2024-04-27T16:05:39.790184+00:00 | 2024-04-27T16:43:37.500831+00:00 | 242 | false | # Intuition\n\n\n\n\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n```C++ []\nclass Solution {\npublic:\n bool canMakeSquare(vector<vector<char>>& grid) {\n int cnt = 0, flag = 0;\n for(int i = 0; i < 2; i++){\n for(int j = 0; j < 2; j++){\n if(grid[i][j] == \'B\'){\n if(grid[i][j + 1] == \'W\')\n cnt++;\n if(grid[i + 1][j] == \'W\')\n cnt++;\n if(grid[i + 1][j + 1] == \'W\')\n cnt++;\n if(cnt == 0)\n cnt++;\n }\n if(grid[i][j] == \'W\'){\n if(grid[i][j + 1] == \'B\')\n cnt++;\n if(grid[i + 1][j] == \'B\')\n cnt++;\n if(grid[i + 1][j + 1] == \'B\')\n cnt++;\n if(cnt == 0)\n cnt++;\n }\n if(cnt == 1 or cnt == 3){\n flag = 1;\n break;\n }\n else\n cnt = 0;\n }\n }\n if(flag == 1)\n return true;\n return false;\n }\n};\n```\n```python3 []\nclass Solution:\n def canMakeSquare(self, grid: List[List[str]]) -> bool:\n for si in range(2):\n for sj in range(2):\n nb = 0\n for i in range(2):\n for j in range(2):\n if grid[si+i][sj+j] == \'B\':\n nb += 1\n if nb in {0,1,3,4}:\n return True\n return False\n \n```\n```C []\nbool canMakeSquare(char** grid, int gridSize, int* gridColSize) {\n int n = gridSize;\n int m = (*gridColSize);\n for (int i = 0; i < n - 1; i++) {\n for (int j = 0; j < m - 1; j++) {\n int b = 0, w = 0;\n for (int m1 = 0; m1 < 2; m1++) {\n for (int m2 = 0; m2 < 2; m2++) {\n if (grid[i + m1][j + m2] == \'W\')\n w++;\n else\n b++;\n }\n }\n if (b >= 3 || w >= 3)\n return 1;\n }\n }\n return 0;\n}\n```\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n O(n * m)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n O(1)\n# Code\n```\nclass Solution {\npublic:\n bool canMakeSquare(vector<vector<char>>& grid) {\n int cnt = 0, flag = 0;\n for(int i = 0; i < 2; i++){\n for(int j = 0; j < 2; j++){\n if(grid[i][j] == \'B\'){\n if(grid[i][j + 1] == \'W\')\n cnt++;\n if(grid[i + 1][j] == \'W\')\n cnt++;\n if(grid[i + 1][j + 1] == \'W\')\n cnt++;\n if(cnt == 0)\n cnt++;\n }\n if(grid[i][j] == \'W\'){\n if(grid[i][j + 1] == \'B\')\n cnt++;\n if(grid[i + 1][j] == \'B\')\n cnt++;\n if(grid[i + 1][j + 1] == \'B\')\n cnt++;\n if(cnt == 0)\n cnt++;\n }\n if(cnt == 1 or cnt == 3){\n flag = 1;\n break;\n }\n else\n cnt = 0;\n }\n }\n if(flag == 1)\n return true;\n return false;\n }\n};\n```\n# Please upvote if it\'s useful for you all\n\n | 4 | 1 | ['C', 'Python', 'C++', 'Java', 'Python3'] | 2 |
make-a-square-with-the-same-color | simple and easy understand solution | simple-and-easy-understand-solution-by-s-4roy | if it\'s help, please up \u2B06 vote! \u2764\uFE0F\n\n\n# Code\n\nclass Solution {\npublic:\n bool fun2(vector<vector<char>>& grid, int i, int j)\n {\n | shishirRsiam | NORMAL | 2024-04-27T16:56:39.262445+00:00 | 2024-04-27T16:56:39.262468+00:00 | 173 | false | # if it\'s help, please up \u2B06 vote! \u2764\uFE0F\n\n\n# Code\n```\nclass Solution {\npublic:\n bool fun2(vector<vector<char>>& grid, int i, int j)\n {\n return (grid[i][j] == grid[i][j+1] and grid[i][j] == grid[i-1][j+1]);\n }\n bool fun(vector<vector<char>>& grid, int i, int j)\n {\n bool option1 = (grid[i][j] == grid[i+1][j] and grid[i][j] == grid[i][j+1]);\n bool option2 = (grid[i][j] == grid[i][j+1] and grid[i][j] == grid[i+1][j+1]);\n bool option3 = (grid[i][j] == grid[i+1][j] and grid[i][j] == grid[i+1][j+1]);\n return option1 or option2 or option3;\n }\n bool canMakeSquare(vector<vector<char>>& grid) \n {\n bool flag = false;\n for(int i=0;i<2;i++)\n {\n for(int j=0;j<2;j++)\n if(fun(grid, i, j)) return true;\n }\n\n for(int i=1;i<3;i++)\n {\n for(int j=0;j<2;j++)\n if(fun2(grid, i, j)) return true;\n }\n return false;\n }\n};\n``` | 3 | 0 | ['String', 'Matrix', 'Simulation', 'C++'] | 3 |
make-a-square-with-the-same-color | Easy to Understand | O(1) | 0ms Runtime | Beats 100%🔥😎 | easy-to-understand-o1-0ms-runtime-beats-h27uh | IntuitionCheck if a 2X2 grid for the same color can be formed.ApproachIterate over each cell and check if consecutive one is same.Complexity
Time complexity: O( | tyagideepti9 | NORMAL | 2025-01-27T17:15:10.784530+00:00 | 2025-01-27T17:15:10.784530+00:00 | 66 | false | # Intuition
Check if a 2X2 grid for the same color can be formed.
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
Iterate over each cell and check if consecutive one is same.
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity: O(1)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```csharp []
public class Solution {
public bool CanMakeSquare(char[][] grid) {
for(int i = 0; i < 3; i++)
{
for(int j = 1; j < 3; j++)
{
if(grid[i][j] == grid[i][j - 1])
{
if(i == 0)
{
if(grid[i + 1][j] == grid[i][j] || grid[i + 1][j - 1] == grid[i][j])
{
return true;
}
}
else if(i == 1)
{
if(grid[i + 1][j] == grid[i][j] || grid[i + 1][j - 1] == grid[i][j])
{
return true;
}
if(grid[i - 1][j] == grid[i][j] || grid[i - 1][j - 1] == grid[i][j])
{
return true;
}
}
else
{
if(grid[i - 1][j] == grid[i][j] || grid[i - 1][j - 1] == grid[i][j])
{
return true;
}
}
}
}
}
return false;
}
}
``` | 2 | 0 | ['C#'] | 1 |
make-a-square-with-the-same-color | Solution in C | solution-in-c-by-akcyyix0sj-7b38 | Code | vickyy234 | NORMAL | 2025-01-06T14:57:21.194775+00:00 | 2025-01-06T14:57:21.194775+00:00 | 63 | false | # Code
```c []
bool canMakeSquare(char** grid, int gridSize, int* gridColSize) {
*gridColSize = 3;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
int w = 0, b = 0;
if (grid[i][j] == 'W')
w++;
else
b++;
if (grid[i][j + 1] == 'W')
w++;
else
b++;
if (grid[i + 1][j] == 'W')
w++;
else
b++;
if (grid[i + 1][j + 1] == 'W')
w++;
else
b++;
if (w >= 3 || b >= 3)
return true;
}
}
return false;
}
``` | 2 | 0 | ['C'] | 0 |
make-a-square-with-the-same-color | Easy code using 2 If cases only. | easy-code-using-2-if-cases-only-by-sapil-adey | We just traverse through the matrix and look for a 2x2 square by checking if any of the ith element i+1th element , j+1th element and the diagonal / (i+1,j+1)th | LeadingTheAbyss | NORMAL | 2024-11-25T20:26:42.756438+00:00 | 2024-11-25T20:42:15.368468+00:00 | 54 | false | We just traverse through the matrix and look for a `2x2` square by checking if any of the `i`th element `i+1`th element , `j+1`th element and the `diagonal` / `(i+1,j+1)`th element is of the same color , if they are then we can make one change to make a `2x2` box of same color else we can not.\n\n# Code\n```cpp []\nclass Solution {\npublic:\n bool canMakeSquare(vector<vector<char>>& grid) {\n for (int i = 0 ; i < grid.size()-1 ;i++) {\n for (int j = 0 ; j<grid[0].size()-1;j++) {\n if (iswhite(grid[i][j]) + iswhite(grid[i + 1][j]) + iswhite(grid[i][j + 1]) + iswhite(grid[i + 1][j + 1]) >= 3)\n return true;\n if ((isblack(grid[i][j]) + isblack(grid[i + 1][j]) + isblack(grid[i][j + 1]) + isblack(grid[i + 1][j + 1])) >= 3) \n return true;\n }\n }\n return false;\n }\n private: \n bool iswhite(char a) //Checks for white\n { return a==\'W\'?true:false; \n}\n bool isblack(char a) // Checks for black \n { return a ==\'B\'?true:false; \n}\n};\n``` | 2 | 0 | ['C++'] | 0 |
make-a-square-with-the-same-color | Beats 100% of Java + 99% Memory - Hardcoded | beats-100-of-java-99-memory-hardcoded-by-zt2p | Intuition\nIf the problem is small, make it efficient.\n\n# Approach\nWithin the constraints of the problem, outside that this is completely unscalable.\n\n# Co | taylorthurman | NORMAL | 2024-05-10T03:07:49.042879+00:00 | 2024-05-10T03:07:49.042898+00:00 | 154 | false | # Intuition\nIf the problem is small, make it efficient.\n\n# Approach\nWithin the constraints of the problem, outside that this is completely unscalable.\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(n)$$\n\n# Code\n```\nclass Solution {\n public boolean canMakeSquare(char[][] grid) {\n\n if(grid[0][0] + grid[0][1] + grid[1][0] + grid[1][1] == 285 \n || grid[0][0] + grid[0][1] + grid[1][0] + grid[1][1] == 264 \n || grid[0][0] + grid[0][1] + grid[1][0] + grid[1][1] == 327 \n || grid[0][0] + grid[0][1] + grid[1][0] + grid[1][1] == 348) { return true; }\n\n if(grid[0][1] + grid[0][2] + grid[1][1] + grid[1][2] == 285 \n || grid[0][1] + grid[0][2] + grid[1][1] + grid[1][2] == 264 \n || grid[0][1] + grid[0][2] + grid[1][1] + grid[1][2] == 327 \n || grid[0][1] + grid[0][2] + grid[1][1] + grid[1][2] == 348) { return true; }\n\n if(grid[1][0] + grid[1][1] + grid[2][0] + grid[2][1] == 285 \n || grid[1][0] + grid[1][1] + grid[2][0] + grid[2][1] == 264 \n || grid[1][0] + grid[1][1] + grid[2][0] + grid[2][1] == 327 \n || grid[1][0] + grid[1][1] + grid[2][0] + grid[2][1] == 348) { return true; }\n\n if(grid[1][1] + grid[1][2] + grid[2][1] + grid[2][2] == 285 \n || grid[1][1] + grid[1][2] + grid[2][1] + grid[2][2] == 264 \n || grid[1][1] + grid[1][2] + grid[2][1] + grid[2][2] == 327 \n || grid[1][1] + grid[1][2] + grid[2][1] + grid[2][2] == 348) { return true; }\n\n return false;\n }\n}\n``` | 2 | 1 | ['Java'] | 0 |
make-a-square-with-the-same-color | "Beats 100%: Grid Transformation Trick! 🎨✨" | beats-100-grid-transformation-trick-by-c-fo6i | Intuition\nWhen faced with the task of determining if it\'s possible to create a 2x2 square of the same color in a given grid with the option to change the colo | CosmixCode | NORMAL | 2024-05-01T17:14:30.672286+00:00 | 2024-05-01T17:14:30.672311+00:00 | 142 | false | # Intuition\nWhen faced with the task of determining if it\'s possible to create a 2x2 square of the same color in a given grid with the option to change the color of at most one cell, we can approach the problem by observing the properties of such squares and devising a strategy to identify them.\n\n1. Understanding 2x2 Squares:\n \n - To form a 2x2 square of the same color, we need to identify a cell and its adjacent cells (to the right, bottom, and bottom-right diagonal) with the same color.\n - This implies that in any such square, at least three of the four cells must have the same color.\n\n2. Flexible Color Change:\n\n - This means that if we encounter a 2x2 square with only one cell of a different color, we can potentially change its color to match the surrounding cells, thus forming a square of the same color.\n\n---\n\n# Approach\n1. Iterate Through the Grid:\n\n - We traverse the 2D grid row by row and column by column, examining each cell.\n - We iterate up to the second last row and the second last column, as checking these cells and their adjacent cells is sufficient to cover all possible 2x2 squares within the grid.\n\n2. Check Adjacent Cells:\n\n - For each cell (i, j) in the grid, we examine its adjacent cells to see if they form a square of the same color.\n - We check the cell to the right (i, j+1), the cell below (i+1, j), and the cell to the bottom-right diagonal (i+1, j+1).\n\n3. Count Same Colored Adjacent Cells:\n\n - We count the number of adjacent cells that have the same color as the current cell (i, j).\n - If the count is either 1 (indicating no adjacent cells of the same color) or 3 or 4 (indicating a square can be formed), we return true.\n\n4. Return False if No Square Found:\n\n - If we traverse the entire grid and cannot find any 2x2 square of the same color, we return false.\n\n---\n\n**IMPORTANT NOTE: **\n\n- Since the task is to determine if there exists a 2x2 square of the same color, you only need to check up to the second last column and the second last row.\n\n- When you\'re at grid[i][j], you\'re effectively checking the square formed by grid[i][j], grid[i][j+1], grid[i+1][j], and grid[i+1][j+1]. You can see that you\'re already covering all possible 2x2 squares within the grid when you iterate up to the second last column (n-1) and the second last row (n-1). \n- Therefore, iterating up to n - 1 for both rows and columns is sufficient to cover all possible 2x2 squares within the grid. There\'s no need to iterate until the last row and the last column.\n\n---\n\n# Complexity\n- Time complexity:\n\nLet\'s denote m as the number of rows and n as the number of columns in the grid.\nThe nested loops iterate over each cell in the grid, resulting in a time complexity of O(m*n).\n- Space complexity:\n\nThe algorithm uses only a constant amount of extra space for variables, resulting in O(1) space complexity.\n# Code\n```\nclass Solution {\n public boolean canMakeSquare(char[][] grid) {\n \n int n = grid.length-1;\n\n for(int i=0; i<n; i++){\n for(int j=0; j<n; j++){\n int count = 1;\n\n if (grid[i][j]==grid[i][j+1]) count++;\n if (grid[i][j]==grid[i+1][j]) count++;\n if (grid[i][j]==grid[i+1][j+1]) count++;\n\n if (count==1 || count==3 || count==4) return true;\n }\n }\n return false;\n }\n}\n``` | 2 | 0 | ['Java'] | 0 |
make-a-square-with-the-same-color | ⚡Clean & Best Code💯 || 🌟O(1) SC | clean-best-code-o1-sc-by-adish_21-5mhg | Complexity\n\n- Time complexity:\nO(1)\n\n- Space complexity:\nO(1)\n\n\n# Code\n## Please Upvote if it helps\uD83E\uDD17\n\nclass Solution {\npublic:\n bool | aDish_21 | NORMAL | 2024-04-30T18:47:46.624054+00:00 | 2024-04-30T18:47:46.624079+00:00 | 204 | false | # Complexity\n```\n- Time complexity:\nO(1)\n\n- Space complexity:\nO(1)\n```\n\n# Code\n## Please Upvote if it helps\uD83E\uDD17\n```\nclass Solution {\npublic:\n bool canMakeSquare(vector<vector<char>>& grid) {\n for(int i = 0 ; i < 2 ; i++){\n for(int j = 0 ; j < 2 ; j++){\n int B_cnt = 0;\n if(grid[i][j] == \'B\')\n B_cnt++;\n if(grid[i][j + 1] == \'B\')\n B_cnt++;\n if(grid[i + 1][j] == \'B\')\n B_cnt++;\n if(grid[i + 1][j + 1] == \'B\')\n B_cnt++;\n if(B_cnt == 1 || B_cnt == 3 || B_cnt == 4 || B_cnt == 0)\n return true; \n }\n }\n return false;\n }\n};\n``` | 2 | 0 | ['Matrix', 'C++'] | 0 |
make-a-square-with-the-same-color | Easy clean C++ code | easy-clean-c-code-by-omsl9850-umw7 | Intuition\nThe problem seems to involve checking whether it\'s possible to form a square by selecting a 2x2 subgrid in the given grid where at least three cells | omsl9850 | NORMAL | 2024-04-30T14:34:02.311491+00:00 | 2024-04-30T14:34:02.311512+00:00 | 36 | false | # Intuition\nThe problem seems to involve checking whether it\'s possible to form a square by selecting a 2x2 subgrid in the given grid where at least three cells have the same color.\n\n# Approach\nThe approach here is to iterate through all possible 2x2 subgrids in the grid. For each subgrid, count the number of white (\'W\') and black (\'B\') cells. If either the count of white or black cells is greater than or equal to 3, return true, indicating that a square can be formed. Otherwise, return false.\n\n# Complexity\n- Time complexity: `O(m x n)`, where \\( m \\) is the number of rows and \\( n \\) is the number of columns in the grid.\n- Space complexity: `O(1)`, as we are using a constant amount of extra space.\n\n# Code\n```cpp\nclass Solution {\npublic:\n bool findSameColor(int rowInd, int colInd, vector<vector<char>>& grid){\n int maxCount = 0;\n int wCount = 0;\n int bCount = 0;\n for(int row=rowInd; row<rowInd+2; row++){\n for(int col=colInd; col<colInd+2; col++){\n if(grid[row][col] == \'W\'){\n wCount++;\n }else {\n bCount++;\n }\n }\n }\n \n maxCount = max(wCount, bCount);\n return (maxCount >= 3);\n }\n \n bool canMakeSquare(vector<vector<char>>& grid) {\n return(findSameColor(0, 0, grid) || findSameColor(0, 1, grid) || findSameColor(1, 0, grid) || findSameColor(1, 1, grid));\n }\n};\n```\n\n\n> \u2705Please Upvote \u2B06 | 2 | 0 | ['C++'] | 0 |
make-a-square-with-the-same-color | 3127. 100% Minimalistic Python Solution | 3127-100-minimalistic-python-solution-by-siu8 | py\nclass Solution:\n def canMakeSquare(self, grid: List[List[str]]) -> bool:\n for i in range(2):\n for j in range(2):\n su | ssvadim | NORMAL | 2024-04-27T21:35:25.904681+00:00 | 2024-04-27T21:35:25.904712+00:00 | 166 | false | ```py\nclass Solution:\n def canMakeSquare(self, grid: List[List[str]]) -> bool:\n for i in range(2):\n for j in range(2):\n submatrix = (\n grid[i][j],\n grid[i + 1][j],\n grid[i][j + 1],\n grid[i + 1][j + 1],\n )\n if sum([cell == "B" for cell in submatrix]) != 2:\n return True\n\n return False\n\n``` | 2 | 0 | ['Python3'] | 1 |
make-a-square-with-the-same-color | C++ || BRUTE FORCE ✅✅✅💯💯💯🔥🔥🔥 | c-brute-force-by-_kvschandra_2234-a26c | \n# Code\n\nbool check(unordered_map<char, int>& mp) {\n return (mp[\'W\'] == 3 && mp[\'B\'] == 1) || (mp[\'B\'] == 3 && mp[\'W\'] == 1) || mp[\'B\'] == 4 || | _kvschandra_2234 | NORMAL | 2024-04-27T16:02:40.051672+00:00 | 2024-04-27T16:04:01.285966+00:00 | 12 | false | \n# Code\n```\nbool check(unordered_map<char, int>& mp) {\n return (mp[\'W\'] == 3 && mp[\'B\'] == 1) || (mp[\'B\'] == 3 && mp[\'W\'] == 1) || mp[\'B\'] == 4 || mp[\'W\'] == 4;\n}\n\nclass Solution {\npublic:\n bool canMakeSquare(vector<vector<char>>& grid) {\n unordered_map<char, int> mp1, mp2, mp3, mp4;\n mp1[grid[0][0]]++;\n mp1[grid[0][1]]++;\n mp1[grid[1][1]]++;\n mp1[grid[1][0]]++;\n\n mp2[grid[1][0]]++;\n mp2[grid[1][1]]++;\n mp2[grid[2][1]]++;\n mp2[grid[2][0]]++;\n\n mp3[grid[1][1]]++;\n mp3[grid[1][2]]++;\n mp3[grid[2][2]]++;\n mp3[grid[2][1]]++;\n\n mp4[grid[1][1]]++;\n mp4[grid[1][2]]++;\n mp4[grid[0][2]]++;\n mp4[grid[0][1]]++;\n\n if(check(mp1) || check(mp2) || check(mp3) || check(mp4)) return true;\n \n return false;\n }\n\n};\n\n``` | 2 | 0 | ['C++'] | 0 |
make-a-square-with-the-same-color | c++[easy] 🎊 🔥 | ceasy-by-varuntyagig-5gf8 | Code | varuntyagig | NORMAL | 2025-03-06T09:32:51.655040+00:00 | 2025-03-06T09:32:51.655040+00:00 | 44 | false | # Code
```cpp []
class Solution {
public:
void calculateSum(vector<vector<char>>& grid, int x1, int y1, int x2,
int y2, int& sum) {
for (int i = x1; i < x2; ++i) {
for (int j = y1; j < y2; ++j) {
sum += grid[i][j];
}
}
}
bool canMakeSquare(vector<vector<char>>& grid) {
for (int i = 0; i < grid.size(); ++i) {
for (int j = 0; j < grid[i].size(); ++j) {
if (grid[i][j] == 'B') {
grid[i][j] = '0';
} else {
grid[i][j] = '1';
}
}
}
// 1-four
int sum = 0;
calculateSum(grid, 0, 0, 2, 2, sum);
if (sum == 196 || sum == 195 || sum == 192 || sum == 193) {
return true;
}
// 2-four
sum = 0;
calculateSum(grid, 0, 1, 2, 3, sum);
if (sum == 196 || sum == 195 || sum == 192 || sum == 193) {
return true;
}
// 3-four
sum = 0;
calculateSum(grid, 1, 0, 3, 2, sum);
if (sum == 196 || sum == 195 || sum == 192 || sum == 193) {
return true;
}
// 4-four
sum = 0;
calculateSum(grid, 1, 1, 3, 3, sum);
if (sum == 196 || sum == 195 || sum == 192 || sum == 193) {
return true;
}
return false;
}
};
``` | 1 | 0 | ['Array', 'Matrix', 'C++'] | 0 |
make-a-square-with-the-same-color | ☑️ Checking if it is possible to Make a Square with the Same Color. ☑️ | checking-if-it-is-possible-to-make-a-squ-un08 | Code | Abdusalom_16 | NORMAL | 2025-02-21T05:51:19.997542+00:00 | 2025-02-21T05:51:19.997542+00:00 | 24 | false | # Code
```dart []
class Solution {
bool canMakeSquare(List<List<String>> grid) {
List<List<String>> list = [];
for(int i = 1; i < grid.length-1; i++){
list.add([grid[i][i-1], grid[i][i], grid[i-1][i-1], grid[i-1][i] ]);
list.add([grid[i][i], grid[i][i+1], grid[i-1][i], grid[i-1][i+1] ]);
list.add([grid[i][i-1], grid[i][i], grid[i+1][i-1], grid[i+1][i] ]);
list.add([grid[i][i], grid[i][i+1], grid[i+1][i], grid[i+1][i+1] ]);
}
for(var elem in list){
int countB = 0;
for(String innerElem in elem){
if(innerElem == "B"){
countB++;
}
}
if(countB >= 3 || countB <= 1){
return true;
}
}
return false;
}
}
``` | 1 | 0 | ['Array', 'Matrix', 'Enumeration', 'Dart'] | 0 |
make-a-square-with-the-same-color | Python Solution | python-solution-by-nirmal1234-p3hw | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | Nirmal_Manoj | NORMAL | 2025-01-27T17:45:28.712163+00:00 | 2025-01-27T17:45:28.712163+00:00 | 41 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```python3 []
class Solution:
def canMakeSquare(self, grid: List[List[str]]) -> bool:
top_left=[grid[0][0],grid[0][1],grid[1][0],grid[1][1]]
top_right=[grid[0][1],grid[0][2],grid[1][1],grid[1][2]]
bottom_left=[grid[1][0],grid[1][1],grid[2][0],grid[2][1]]
bottom_right=[grid[1][1],grid[1][2],grid[2][1],grid[2][2]]
lst=[top_left, top_right, bottom_left, bottom_right]
for i in lst:
if i.count('B') != i.count('W'):
return True
return False
``` | 1 | 0 | ['Python3'] | 1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.