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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
consecutive-characters | c++ simple solution | c-simple-solution-by-ron970404-j6km | \tclass Solution {\n\tpublic:\n\t\tint maxPower(string s) {\n\t\t\tint maxlength = 1;\n\t\t\tint len = 1;\n\t\t\tfor(int i = 1; i < s.length(); i++){\n\t\t\t\ti | ron970404 | NORMAL | 2021-03-01T07:23:33.420279+00:00 | 2021-03-01T07:23:33.420340+00:00 | 219 | false | \tclass Solution {\n\tpublic:\n\t\tint maxPower(string s) {\n\t\t\tint maxlength = 1;\n\t\t\tint len = 1;\n\t\t\tfor(int i = 1; i < s.length(); i++){\n\t\t\t\tif(s[i] != s[i-1]) len = 1;\n\t\t\t\telse len++;\n\t\t\t\tmaxlength = max(len,maxlength);\n\t\t\t}\n\t\t\treturn maxlength;\n\t\t}\n\t}; | 4 | 0 | ['C'] | 0 |
consecutive-characters | SImple Python solution using an additional array | simple-python-solution-using-an-addition-571q | \narr=[1]*len(s)\nfor i in range(1,len(s)):\n\tif s[i]==s[i-1]:\n\t\tarr[i]=arr[i-1]+1\nreturn max(arr) \n | praveenpurwar2004 | NORMAL | 2020-07-17T05:03:59.776847+00:00 | 2020-07-17T05:06:00.699768+00:00 | 213 | false | ```\narr=[1]*len(s)\nfor i in range(1,len(s)):\n\tif s[i]==s[i-1]:\n\t\tarr[i]=arr[i-1]+1\nreturn max(arr) \n``` | 4 | 0 | [] | 0 |
consecutive-characters | Beginners friendly ..!! | beginners-friendly-by-amruthakm77-hsy1 | 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 | Amruthakm77 | NORMAL | 2024-11-22T04:25:18.519075+00:00 | 2024-11-22T04:25:18.519119+00:00 | 124 | 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```javascript []\n/**\n * @param {string} s\n * @return {number}\n */\nvar maxPower = function(s) {\n let max=0;\n let count =1;\n for(let i=1;i<s.length;i++){\n if(s[i] == s[i-1]){\n count++\n }\n else {\n max = Math.max(count, max)\n count=1;\n }\n }\n \n max = Math.max(max,count);\n console.log(max)\n return max;\n};\n\n``` | 3 | 0 | ['JavaScript'] | 1 |
consecutive-characters | easy hai ek loop mein ho jayega kar do jaldi solve :D | easy-hai-ek-loop-mein-ho-jayega-kar-do-j-ea3z | 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 | yesyesem | NORMAL | 2024-09-09T18:49:01.670982+00:00 | 2024-09-09T18:49:01.671010+00:00 | 104 | 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```cpp []\nclass Solution {\npublic:\n int maxPower(string s) {\n int c=0;\n int max=0;\n for(int i=0;i<s.length()-1;i++)\n {\n if(s[i]==s[i+1])\n c++;\n else\n c=0;\n\n if(c>max)\n max=c;\n }\n return max+1;\n }\n};\n``` | 3 | 0 | ['C++'] | 0 |
consecutive-characters | Beats 100% of users with C++ || Step By Step Explain || Best Approach || Easy to Understand || | beats-100-of-users-with-c-step-by-step-e-10ax | Abhiraj Pratap Singh\n\n---\n\n# if you like the solution please UPVOTE it.....\n\n---\n\n# Intuition\n Describe your first thoughts on how to solve this proble | abhirajpratapsingh | NORMAL | 2024-04-21T17:44:07.196139+00:00 | 2024-04-21T17:44:07.196169+00:00 | 66 | false | # Abhiraj Pratap Singh\n\n---\n\n# if you like the solution please UPVOTE it.....\n\n---\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- Traverse the string while keeping track of consecutive counts of repeating characters.\n\n---\n\n\n\n---\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Initialize variables ans and count to 1.\n- Iterate through the string starting from index 1.\n- If the current character is equal to the previous character, increment count.\n- If the current character is different from the previous character:\n - Update ans to be the maximum of ans and count.\n - Reset count to 1.\n- After the loop, compare ans and count and return the maximum value.\n\n---\n\n# Complexity\n- **Time complexity :** O(n), where n is the length of the input string. This is because the algorithm iterates through the string once.\n- **Space complexity :** O(1). The algorithm uses a constant amount of extra space regardless of the input size.\n\n---\n\n# Code\n```\nclass Solution {\npublic:\n int maxPower(string s) \n {\n int ans = 1 ;\n int count = 1 ;\n for ( int i = 1 ; i < s.length() ; i++ )\n {\n if ( s[i] == s[i-1] )\n count++ ;\n else\n {\n ans = max ( ans , count ) ;\n count = 1 ;\n }\n }\n if ( ans < count ) \n return count ;\n return ans ;\n }\n};\n```\n\n---\n\n# if you like the solution please UPVOTE it.....\n\n---\n\n\n\n\n\n\n\n---\n\n--- | 3 | 0 | ['String', 'C++'] | 0 |
consecutive-characters | Beats 100.00%%🔥✅|| easy JAVA Solution✅ | beats-10000-easy-java-solution-by-elockl-4elx | Intuition\n#### we need to find the Longest Substring with the same characters.\n\n\n# Approach\n\n\nWe can iterate over the given string, and use a variable te | elocklymuhamed | NORMAL | 2024-04-17T14:23:20.221494+00:00 | 2024-04-17T14:23:55.095429+00:00 | 211 | false | # Intuition\n#### we need to find the Longest Substring with the same characters.\n\n\n# Approach\n\n\nWe can iterate over the given string, and use a variable tempLength to record the length of that substring.\n\nWhen the next character is the same as the previous one, we increase tempLength by one. Else, we reset tempLength to 1.\nand check if tempLength is greater than maxLength or not\n\nWith this method, when reaching the end of a substring with the same characters, tempLength will be the length of that substring, since we reset the tempLength when that substring starts, and increase tempLength when iterate that substring.\n\nTherefore, the maximum value of tempLength is what we need.\n# Complexity\n- Time complexity:\n O(N), since we perform one loop through `s`\n- Space complexity:\nO(1), since we only have two integer variables `maxLength` and `tempLength` \n\n### Finally : If you like the method of solution and explanation, do not hesitate to vote up to encourage me to solve more problems and solve them in better ways.\n# Code\n```\nclass Solution {\n public int maxPower(String s) {\n int maxLength = 1;\n int tempLength = 1;\n for (int i = 1; i < s.length(); i++) {\n if (s.charAt(i) == s.charAt(i - 1)) {\n tempLength++;\n } else {\n if (tempLength > maxLength) {\n maxLength = tempLength;\n }\n tempLength = 1;\n }\n }\n if(tempLength>maxLength){\n maxLength=tempLength;\n }\n return maxLength;\n }\n}\n```\n\n | 3 | 0 | ['Java'] | 0 |
consecutive-characters | 1446. Consecutive Characters: Easy to Understand ✔✔ | 1446-consecutive-characters-easy-to-unde-ujty | Code\n\nclass Solution {\npublic:\n int maxPower(string s) {\n int currCount = 1, power = 0;\n for (int i = 1; i < s.size(); i++)\n {\n | jasneet_aroraaa | NORMAL | 2023-12-30T07:04:02.562036+00:00 | 2023-12-30T07:04:02.562059+00:00 | 41 | false | # Code\n```\nclass Solution {\npublic:\n int maxPower(string s) {\n int currCount = 1, power = 0;\n for (int i = 1; i < s.size(); i++)\n {\n if (s[i - 1] == s[i]) currCount++;\n else\n {\n power = max(currCount, power);\n currCount = 1;\n }\n }\n power = max(currCount, power);\n return power;\n }\n};\n``` | 3 | 0 | ['C++'] | 0 |
consecutive-characters | Easy JAVA solution!!! 1ms Beats 100%!!! | easy-java-solution-1ms-beats-100-by-elda-owyx | 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 | eldarst | NORMAL | 2023-05-21T09:04:55.359954+00:00 | 2023-05-21T09:04:55.359984+00:00 | 104 | 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<!-- 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 int maxPower(String s) {\n int maxCount = 0, pointer = 0, counter = 0;\n char currChar = s.charAt(pointer);\n\n while (pointer < s.length()) {\n if (s.charAt(pointer) == currChar) {\n ++counter;\n maxCount = Math.max(counter, maxCount);\n } else {\n counter = 1;\n currChar = s.charAt(pointer);\n }\n ++pointer;\n }\n return maxCount;\n }\n}\n``` | 3 | 0 | ['Java'] | 0 |
consecutive-characters | Easy Solution in Python || Beats 70%run time | easy-solution-in-python-beats-70run-time-ygxl | Code\n\nclass Solution(object):\n def maxPower(self, s):\n """\n :type s: str\n :rtype: int\n """\n stack=[]\n mxpo | Lalithkiran | NORMAL | 2023-02-16T15:30:31.254418+00:00 | 2023-02-16T15:30:31.254584+00:00 | 574 | false | # Code\n```\nclass Solution(object):\n def maxPower(self, s):\n """\n :type s: str\n :rtype: int\n """\n stack=[]\n mxpow=0\n for i in s:\n if stack and stack[-1]!=i:\n mxpow=max(mxpow,len(stack))\n stack=[]\n stack.append(i)\n else:\n stack.append(i)\n mxpow=max(mxpow,len(stack))\n return mxpow\n``` | 3 | 0 | ['Python'] | 1 |
consecutive-characters | 📌 c++ solution using sliding window ✔ | c-solution-using-sliding-window-by-1911u-stxy | \nclass Solution {\npublic:\n int maxPower(string s) {\n int i=0,j=0,ans=0,ln=s.size();\n while(j<ln){\n while(j<ln && s[j]==s[i])j+ | 1911Uttam | NORMAL | 2023-02-06T19:21:54.831084+00:00 | 2023-02-06T19:21:54.831118+00:00 | 426 | false | ```\nclass Solution {\npublic:\n int maxPower(string s) {\n int i=0,j=0,ans=0,ln=s.size();\n while(j<ln){\n while(j<ln && s[j]==s[i])j++;\n ans= max(ans, j-i);\n i=j;\n }\n return ans;\n }\n};\n``` | 3 | 0 | ['C', 'Sliding Window', 'C++'] | 0 |
consecutive-characters | easy c++ solution | easy-c-solution-by-bhupendraj9-mpq8 | \nclass Solution {\npublic:\n int maxPower(string s) {\n int count=0;\n int maxcount=0;\n for(int i=0;i<s.size()-1;i++)\n {\n | bhupendraj9 | NORMAL | 2022-11-09T08:18:03.153495+00:00 | 2022-11-09T08:18:03.153535+00:00 | 536 | false | ```\nclass Solution {\npublic:\n int maxPower(string s) {\n int count=0;\n int maxcount=0;\n for(int i=0;i<s.size()-1;i++)\n {\n if(s[i+1]==s[i])\n count++;\n else\n count=0;\n maxcount=max(maxcount,count);\n }\n return maxcount+1;\n }\n};\n``` | 3 | 0 | ['String', 'C'] | 1 |
consecutive-characters | JAVA | consecutive-characters | java-consecutive-characters-by-venkat089-ee47 | \nclass Solution {\n public int maxPower(String s) {\n int res=1,c=1;\n for(int i=0;i<s.length()-1;i++)\n {\n if(s.charAt(i)= | Venkat089 | NORMAL | 2022-11-07T06:58:01.474998+00:00 | 2022-11-07T06:58:01.475034+00:00 | 2,240 | false | ```\nclass Solution {\n public int maxPower(String s) {\n int res=1,c=1;\n for(int i=0;i<s.length()-1;i++)\n {\n if(s.charAt(i)==s.charAt(i+1))c++;\n else {\n res=Math.max(res,c);\n c=1;\n }\n }res=Math.max(res,c);\n return res;\n }\n}\n``` | 3 | 0 | ['Java'] | 0 |
consecutive-characters | JS easy solution with O(n) | js-easy-solution-with-on-by-kunkka1996-4szs | \nvar maxPower = function(s) {\n let output = 1;\n let currentPower = 1;\n \n for (let i = 0; i < s.length; i++) {\n if (s[i] === s[i+1]) {\n | kunkka1996 | NORMAL | 2022-10-12T09:23:27.993955+00:00 | 2022-10-12T09:23:27.993989+00:00 | 541 | false | ```\nvar maxPower = function(s) {\n let output = 1;\n let currentPower = 1;\n \n for (let i = 0; i < s.length; i++) {\n if (s[i] === s[i+1]) {\n currentPower++;\n } else {\n output = Math.max(output, currentPower);\n currentPower = 1;\n }\n }\n \n return output;\n};\n``` | 3 | 0 | ['JavaScript'] | 0 |
consecutive-characters | Go fast O(n) 4 ms | go-fast-on-4-ms-by-tuanbieber-obvs | \nfunc maxPower(s string) int {\n if len(s) == 0 {return 0}\n \n var res int\n \n counter, preChar := 1, s[0]\n \n for i := 1; i < len(s); | tuanbieber | NORMAL | 2022-06-21T11:39:30.186184+00:00 | 2022-06-21T11:39:30.186219+00:00 | 189 | false | ```\nfunc maxPower(s string) int {\n if len(s) == 0 {return 0}\n \n var res int\n \n counter, preChar := 1, s[0]\n \n for i := 1; i < len(s); i++ {\n if s[i] == preChar {\n counter++\n \n if counter > res {res = counter}\n } else {\n counter = 1\n preChar = s[i]\n }\n }\n \n if counter > res {res = counter}\n \n return res\n}\n\n``` | 3 | 0 | ['Go'] | 0 |
consecutive-characters | C++ 100% faster using iterators | c-100-faster-using-iterators-by-perfest-rj3b | \nclass Solution {\npublic:\n int maxPower(string s) {\n s += \' \';\n auto unique_char = s.begin();\n uint32_t current_power = 1;\n | perfest | NORMAL | 2021-12-13T19:17:13.808876+00:00 | 2021-12-13T19:17:13.808900+00:00 | 45 | false | ```\nclass Solution {\npublic:\n int maxPower(string s) {\n s += \' \';\n auto unique_char = s.begin();\n uint32_t current_power = 1;\n uint32_t max_power = 0;\n for (auto it = s.begin() + 1; it != s.end(); it++) {\n if (*it != *unique_char) {\n *unique_char = *it;\n if (current_power > max_power) {\n max_power = current_power;\n }\n current_power = 1;\n } else {\n current_power++;\n }\n }\n return max_power;\n }\n};\n``` | 3 | 0 | ['C', 'Iterator'] | 0 |
consecutive-characters | Easy Solution O(N) time and O(1) space complexity | easy-solution-on-time-and-o1-space-compl-k3zl | Steps - \n Keep a max and currentMax variable assigned to 1\n\t Assigned to 1 because the constraint says the length of string is 1 atleast, thus there would 1 | sahilzainuddin | NORMAL | 2021-12-13T06:05:10.574413+00:00 | 2021-12-13T06:05:10.574444+00:00 | 172 | false | **Steps** - \n* Keep a max and currentMax variable assigned to 1\n\t* Assigned to 1 because the constraint says the length of string is 1 atleast, thus there would 1 result atleast\n* Iterate through the string to len - 1 comparing each char to the next one\n\t* If matches then increase the current max by 1\n\t* Else reset the current max to 1 which is the minimum value\n* After each check update the max to be the maximum out of the max and current max\n* return max as answer \n\n**Time Complexity** - O(N)\n**Space Complexity** - O(1)\n```\n public int maxPower(String s) {\n // Set currMax and max as 1\n // As for a string with one character 1 would be the answer\n int currMax = 1;\n int max = 1;\n // Looping through the string checking each char with the one ahead of it\n for(int i = 0; i < s.length() - 1; i++) {\n char temp = s.charAt(i);\n // If char are equal increase the current max by 1\n if(temp == s.charAt(i + 1)) {\n currMax++;\n } else {\n // If char don\'t match, reset the current max to 1\n currMax = 1;\n }\n // After each check update the max to be the maximum\n // out of current max and itself\n max = Math.max(currMax, max);\n }\n // return max as answer\n return max;\n }\n``` | 3 | 1 | ['Java'] | 0 |
consecutive-characters | [Python] Optimisation Process Explained | Beginner-Friendly | python-optimisation-process-explained-be-1ugv | This is a repost since the previous one got aggressively downvoted for some reason... -10 in the span of 1 minute :/ If you dislike this post that much, please | zayne-siew | NORMAL | 2021-12-13T03:36:39.019779+00:00 | 2021-12-13T03:36:39.019811+00:00 | 129 | false | > This is a repost since the previous one got aggressively downvoted for some reason... -10 in the span of 1 minute :/ If you dislike this post that much, please comment below why and help me improve, thank you!\n\n### Introduction\n\nWe are given a string `s` and we want to find its maximum power. To rephrase the problem statement, we need to find the **length of the longest substring in `s` containing only one unique element**.\n\nWhere do we start? First, we should know how to check for a substring that only contains one unique element. This step can be performed recursively; if there exists a substring `sub = s[i:i+n]` where `0 <= i <= i+n < len(s)` that fulfils the criteria - `sub[j] = chr` for `0 <= j < len(sub)` - then there exists a substring `sub = s[i:i+n+1]` that fulfils the criteria if `s[i+n] = ch`.\n\nWhat this tells us is that it is possible to **perform a one-pass through the string `s` to obtain the maximum power**. At each index, we only need to check if `s[i]` is equal to the unique character of the adjacent substring.\n\n---\n\n### Attempt 1: Storing the adjacent substring\n\nSince we need to check the adjacent substring, it will be helpful for us to store it somewhere so that we can retrieve it for checking. The easiest way would be to store the entire substring into a variable. Then, per iteration, we can check if each of the characters in the substring is equal to the current one.\n\n```python\nclass Solution:\n def maxPower(self, s: str) -> int:\n sub, power = \'\', 0\n for ch1 in s:\n # check against each character in sub\n for ch2 in sub:\n if ch1 != ch2:\n # reset sub if characters are not equal\n if len(sub) > power:\n power = len(sub)\n sub = \'\'\n break\n # add the new character to sub\n sub += ch1\n return max(len(sub), power) # in case sub never gets reset\n```\n\n**TC: <img src="https://latex.codecogs.com/png.image?\\dpi{110}&space;O(n^{2})" title="O(n^{2})" />**, due to the nested for loop.\n**SC: <img src="https://latex.codecogs.com/png.image?\\dpi{110}&space;O(n)" title="O(n)" />**, since we store the longest substring as a variable `sub`.\n\n---\n\n### Attempt 2: Evaluating comparison\n\nNote that any `sub` stored will always satisfy the criteria of containing only one unique character. Hence, instead of checking each character in `sub`, we can **check only one character to determine if `sub+ch` satisfies the criteria**. While you can choose to check any character in `sub`, I will be using the last character `sub[-1]` since it is more intuitive.\n\n```python\nclass Solution:\n def maxPower(self, s: str) -> int:\n sub, power = \'\', 0\n for ch in s:\n # sub fulfils the criteria, check if sub+ch also fulfils the criteria\n if sub and sub[-1] == ch:\n sub += ch\n else:\n # reset sub to the new adjacent substring\n if len(sub) > power:\n power = len(sub)\n sub = ch\n return max(len(sub), power) # in case sub never gets reset\n```\n\n**TC: <img src="https://latex.codecogs.com/png.image?\\dpi{110}&space;O(n)" title="O(n)" />**, since we loop through all characters in `s` once.\n**SC: <img src="https://latex.codecogs.com/png.image?\\dpi{110}&space;O(n)" title="O(n)" />**, as discussed previously.\n\n---\n\n### Attempt 3: Represent the substring differently\n\nThe previous solution takes up linear space due to the way that we stored the longest substring. We can do better by **representing the substring as its first and last index**.\n\n```python\nclass Solution:\n def maxPower(self, s: str) -> int:\n first, last, power = -1, -1, 0\n for ch in s:\n # s[first:last] fulfils the criteria, check if s[first:last+1] also fulfils the criteria\n if last > first and s[last] == ch:\n last += 1\n else:\n # reset first and last to the new adjacent substring\n if last-first > power:\n power = last-first\n first, last = last, last+1\n return max(last-first, power) # in case first and last never gets reset\n```\n\n**TC: <img src="https://latex.codecogs.com/png.image?\\dpi{110}&space;O(n)" title="O(n)" />**, as discussed previously.\n**SC: <img src="https://latex.codecogs.com/png.image?\\dpi{110}&space;O(1)" title="O(1)" />**, since we no longer need to store the exact substring.\n\n---\n\n### Attempt 4: Alternative to storing substring\n\nSo far, we have always compared the last element of the substring `sub[-1]` or `s[last]` with the current character to check for equality. If you notice, we have never used the rest of the substring anywhere else in the code, except to determine its length (and power). Therefore, **we can replace the need to store a substring with the last element of the substring and the current power of the substring**.\n\nThis is not really an optimisation per se, but it will help show how we can further optimise the code later on.\n\n```python\nclass Solution:\n def maxPower(self, s: str) -> int:\n curr = power = 1\n prev = s[0]\n for i in range(1, len(s)):\n if s[i] == prev:\n curr += 1\n else:\n if curr > power:\n power = curr\n curr = 1\n prev = s[i]\n return max(curr, power)\n```\n\n**TC: <img src="https://latex.codecogs.com/png.image?\\dpi{110}&space;O(n)" title="O(n)" />**, as discussed previously.\n**SC: <img src="https://latex.codecogs.com/png.image?\\dpi{110}&space;O(1)" title="O(1)" />**, as discussed previously.\n\n---\n\n### Attempt 5: Re-evaluating comparison\n\nThe previous implementation really highlights one area of optimisation: `s[i] == prev`. Notice that `prev = s[i]` gets called in every iteration. This means that **we are actually comparing `s[i-1] == s[i]` for each iteration**. Thus, we can remove the need to store the previous character `prev` entirely, since the character we require for comparison is directly adjacent to the current one.\n\n```python\nclass Solution:\n def maxPower(self, s: str) -> int:\n curr = power = 1\n for i in range(1, len(s)):\n if s[i] == s[i-1]:\n curr += 1\n if curr > power:\n power = curr\n else:\n curr = 1\n return power\n```\n\nFinally, we can compress the code as follows.\n\n```python\nclass Solution:\n def maxPower(self, s: str) -> int:\n curr = power = 1\n for i in range(1, len(s)):\n power = max(power, (curr := curr+1 if s[i] == s[i-1] else 1))\n return power\n```\n\n**TC: <img src="https://latex.codecogs.com/png.image?\\dpi{110}&space;O(n)" title="O(n)" />**, as discussed previously.\n**SC: <img src="https://latex.codecogs.com/png.image?\\dpi{110}&space;O(1)" title="O(1)" />**, as discussed previously.\n\n---\n\n### Conclusion\n\nWe have optimised the code as best as we can. Note that the optimisation may not be reflected in the submission statistics due to fluctuations in code performance.\n\nPlease upvote if this has helped you! Appreciate any comments as well :) | 3 | 5 | ['Python', 'Python3'] | 0 |
consecutive-characters | Python O(n) current pointer approach | python-on-current-pointer-approach-by-va-9svm | \nclass Solution:\n def maxPower(self, s: str) -> int:\n current = 1\n max_freq = 0\n for i in range(1, len(s)):\n if s[i] == | vanigupta20024 | NORMAL | 2020-11-25T12:23:33.177312+00:00 | 2020-11-25T12:23:33.177345+00:00 | 492 | false | ```\nclass Solution:\n def maxPower(self, s: str) -> int:\n current = 1\n max_freq = 0\n for i in range(1, len(s)):\n if s[i] == s[i - 1]:\n current += 1\n else:\n max_freq = max(current, max_freq)\n current = 1\n return max(max_freq, current)\n```\nFor more such questions: https://github.com/vanigupta20024/Programming-Challenges/tree/master | 3 | 0 | ['Python', 'Python3'] | 2 |
consecutive-characters | consecutive character - C++ | consecutive-character-c-by-error_11-9vy9 | \nint maxPower(string s) {\n\n // count is max consecutive characters \n int count= 0;\n\t\n // current temporary maxinum\n\tint current=1;\n\t\n// curren | error_11 | NORMAL | 2020-11-04T06:58:46.550284+00:00 | 2020-11-06T05:09:07.886714+00:00 | 136 | false | ```\nint maxPower(string s) {\n\n // count is max consecutive characters \n int count= 0;\n\t\n // current temporary maxinum\n\tint current=1;\n\t\n// current character \t\n\tchar curr=s[0];\n\t\n// current position of character in string s\t\n\tint i=1;\n\t\n\twhile(i!=s.length()){\n\t\tif(curr==s[i]){\n\t\t\tcurrent++;\n\t\t}else{\n\t\t\tcount=max(count,current);\n\t\t\tcurrent =1;\n\t\t\tcurr=s[i];\n\t\t}\n\t\ti++;\n\t}\n// if there is only length 1 then then we take max of count & current ,where current is 1 \n\treturn max(count,current); \n }\n\t\n```\n\ttime complexity: O(N) | 3 | 0 | ['Linked List', 'C', 'C++'] | 0 |
consecutive-characters | C++ super easy, short and simple One-Pass solution | c-super-easy-short-and-simple-one-pass-s-2hfp | \nclass Solution {\npublic:\n int maxPower(string s) {\n int max_len = 0, curr_len = 0;\n char prev = s[0];\n \n for(auto letter: | yehudisk | NORMAL | 2020-11-03T08:49:51.797438+00:00 | 2020-11-03T08:49:51.797483+00:00 | 161 | false | ```\nclass Solution {\npublic:\n int maxPower(string s) {\n int max_len = 0, curr_len = 0;\n char prev = s[0];\n \n for(auto letter:s){\n if (letter == prev)\n curr_len++;\n else\n curr_len = 1;\n \n max_len = max(max_len, curr_len);\n prev = letter;\n }\n \n return max_len;\n }\n};\n```\n**Like it? please upvote...** | 3 | 0 | ['C'] | 0 |
consecutive-characters | [easy understanding][c++] | easy-understandingc-by-rajat_gupta-rbac | \n//1.[faster than 99.52%]\nclass Solution {\npublic:\n int maxPower(string s) {\n if(s.size()<=1) return s.size();\n int count=1,maxcount=1;\n | rajat_gupta_ | NORMAL | 2020-09-11T21:24:29.815077+00:00 | 2020-09-11T21:24:29.815137+00:00 | 73 | false | ```\n//1.[faster than 99.52%]\nclass Solution {\npublic:\n int maxPower(string s) {\n if(s.size()<=1) return s.size();\n int count=1,maxcount=1;\n for(int i=0;i<s.size()-1;i++){\n if(s[i]==s[i+1]) maxcount=max(maxcount,++count);\n else{\n count=1;\n }\n }\n return maxcount;\n }\n};\n//2.[runtime beats 86.08 %]\nclass Solution {\npublic:\n int maxPower(string s) {\n char c=s[0];\n int count=1,maxcount=1;\n for(int i=1;i<s.size();i++){\n if(s[i]==c) count++;\n else{\n if(count>maxcount) maxcount=count;\n count=1;\n c=s[i];\n }\n }\n if(count>maxcount) maxcount=count;\n return maxcount;\n }\n};\n```\n**Feel free to ask any question in the comment section.**\nI hope that you\'ve found the solution useful.\nIn that case, **please do upvote and encourage me** to on my quest to document all leetcode problems\uD83D\uDE03\nHappy Coding :)\n | 3 | 0 | ['C', 'C++'] | 0 |
consecutive-characters | 1446 | JavaScript 1-line solution | 1446-javascript-1-line-solution-by-spork-kf0j | Runtime: 80 ms, faster than 56.32% of JavaScript online submissions\n> Memory Usage: 36.9 MB, less than 100.00% of JavaScript online submissions\n\njavascript\n | sporkyy | NORMAL | 2020-05-19T12:57:13.972663+00:00 | 2022-01-10T21:07:21.980787+00:00 | 334 | false | > Runtime: **80 ms**, faster than *56.32%* of JavaScript online submissions\n> Memory Usage: **36.9 MB**, less than *100.00%* of JavaScript online submissions\n\n```javascript\nconst maxPower = s =>\n s\n .split(/((\\w)\\2*)/)\n .reduce(\n (max, match) => (max < match.length ? match.length : max),\n -Infinity,\n );\n``` | 3 | 1 | ['JavaScript'] | 1 |
consecutive-characters | Java | 100% | java-100-by-onefineday01-1pj4 | ```\nclass Solution {\n public int maxPower(String s) {\n if(s.length()==0) return 0;\n int n = s.length(), max = 1, curr = 1;\n char pc | onefineday01 | NORMAL | 2020-05-16T17:25:03.704941+00:00 | 2020-05-16T17:25:03.704995+00:00 | 227 | false | ```\nclass Solution {\n public int maxPower(String s) {\n if(s.length()==0) return 0;\n int n = s.length(), max = 1, curr = 1;\n char pc = s.charAt(0);\n for(int i = 1; i < n; i++){\n curr = s.charAt(i) == s.charAt(i-1) ? curr + 1 : 1;\n max = Math.max(curr, max);\n }\n return max;\n }\n} | 3 | 1 | ['Java'] | 0 |
consecutive-characters | Explanation || Complexities || Simple Solution | explanation-complexities-simple-solution-57bd | IntuitionThe goal is to find the maximum consecutive sequence of identical characters in the string.
By identifying groups of consecutive characters, we can eff | Anurag_Basuri | NORMAL | 2025-03-09T06:45:44.571728+00:00 | 2025-03-09T06:45:44.571728+00:00 | 249 | false | # Intuition
The goal is to find the **maximum consecutive sequence** of identical characters in the string.
By identifying groups of consecutive characters, we can efficiently track the maximum sequence length.
# Approach
1. **Initialization:**
- Create a variable `maxLen` to store the maximum consecutive sequence length.
- Use a pointer `i` to iterate through the string.
2. **Counting Consecutive Characters:**
- For each starting character, assign it to `cur`.
- Use a second pointer `j` to count the consecutive identical characters.
- Once the consecutive sequence ends, update `maxLen` with the maximum length found so far.
3. **Advancing the Pointer:**
- Move pointer `i` directly to `j` to skip over the counted sequence efficiently.
4. **Final Answer:**
- Return `maxLen`, which holds the maximum sequence of identical characters.
# Complexity
- **Time Complexity:** $$O(n)$$ — Each character is processed exactly once.
- **Space Complexity:** $$O(1)$$ — Only integer variables are used for counting.
# Code
``` cpp []
class Solution {
public:
int maxPower(string s) {
int maxLen = 0;
int n = s.size();
for(int i = 0; i < n; ){
char cur = s[i];
int j = i, len = 0;
while(j < n && s[j] == cur){
len++;
j++;
}
i = j;
maxLen = max(maxLen, len);
}
return maxLen;
}
};
```
```java []
class Solution {
public int maxPower(String s) {
int maxLen = 0;
int n = s.length();
for (int i = 0; i < n;) {
char cur = s.charAt(i); // Current character
int j = i, len = 0;
// Count consecutive identical characters
while (j < n && s.charAt(j) == cur) {
len++;
j++;
}
i = j; // Move 'i' to the next non-matching character
maxLen = Math.max(maxLen, len);
}
return maxLen;
}
}
```
``` python []
class Solution:
def maxPower(self, s: str) -> int:
maxLen = 0
n = len(s)
i = 0
while i < n:
cur = s[i] # Current character
j = i
length = 0
# Count consecutive identical characters
while j < n and s[j] == cur:
length += 1
j += 1
i = j # Move 'i' to the next non-matching character
maxLen = max(maxLen, length)
return maxLen
``` | 2 | 0 | ['String', 'C++', 'Java', 'Python3'] | 0 |
consecutive-characters | 1446. Consecutive Characters | 1446-consecutive-characters-by-saumyap27-a910 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nsimple approach\n# Comp | saumyap271 | NORMAL | 2024-12-08T02:45:26.003404+00:00 | 2024-12-08T02:45:26.003423+00:00 | 121 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nsimple approach\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```cpp []\nclass Solution {\npublic:\n int maxPower(string s) {\n int count=1,maxcount=1;\n int n=s.length();\n for(int i=0; i<n-1; i++){\n if(s[i]==s[i+1]){\n count++;\n }\n else{\n count=1;\n }\n maxcount=max(maxcount,count);\n }\n return maxcount;\n }\n};\n``` | 2 | 0 | ['C++'] | 0 |
consecutive-characters | Beats 100% runtime simple approach cpp!!! | beats-100-runtime-simple-approach-cpp-by-khag | Intuition\n Describe your first thoughts on how to solve this problem. \nBrute-force\n# Approach\n Describe your approach to solving the problem. \njust using t | parijatb_03 | NORMAL | 2024-06-03T03:00:50.167872+00:00 | 2024-06-03T03:00:50.167889+00:00 | 13 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBrute-force\n# Approach\n<!-- Describe your approach to solving the problem. -->\njust using the concept of max() in cpp , we update the value as long as the same char proceeds along the length and keep it updated in the longest length of occurrance of a char , once a new char comes we start the count from 1 again and follow the same \n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nonly one for loop so O(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int maxPower(string s) {\n int max_length=0;\n int c=1;\n for(int i=1;i<s.length();i++){\n if(s[i-1]==s[i]){\n c++;\n max_length=max(max_length,c);\n }\n else c=1;\n }\n if(max_length<c) max_length=c;\n return max_length;\n }\n};\n``` | 2 | 0 | ['C++'] | 0 |
consecutive-characters | ☑️✅ 100/100 OPTIMIZED Code || Consecutive-characters || Easy Solutions | 100100-optimized-code-consecutive-charac-xoeq | \n\n# Code\n\nclass Solution {\npublic:\n int maxPower(string s) {\n if(s.size()==1)return 1;\n int maxi=INT_MIN;\n for(int i=0;i<s.size | Saiyam_Dubey_ | NORMAL | 2024-05-14T19:10:57.293071+00:00 | 2024-05-14T19:10:57.293094+00:00 | 133 | false | \n\n# Code\n```\nclass Solution {\npublic:\n int maxPower(string s) {\n if(s.size()==1)return 1;\n int maxi=INT_MIN;\n for(int i=0;i<s.size();){\n int count=0;\n int j=i;\n while(s[i]==s[j] && j<s.size()){\n count++;\n j++;\n }\n maxi=max(count,maxi);\n i=j;\n }\n return maxi;\n }\n};\n```\n\n | 2 | 0 | ['String', 'C++'] | 0 |
consecutive-characters | Fastest Solution (100% Beats), Kadane's Approach - Java | fastest-solution-100-beats-kadanes-appro-jtpx | 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 | talhah20343 | NORMAL | 2023-07-30T17:12:51.439052+00:00 | 2023-07-30T17:12:51.439081+00:00 | 438 | 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 int maxPower(String s){\n int ans = 0;\n int k = 0;\n for(int i=0; i<s.length()-1; i++){\n if(s.charAt(i)==s.charAt(i+1)) k++;\n else k=0;\n ans = Math.max(ans, k);\n }\n return Math.max(ans, k)+1;\n }\n}\n``` | 2 | 0 | ['Java'] | 0 |
consecutive-characters | JAVA Solution | Consecutive Characters | 2ms | | java-solution-consecutive-characters-2ms-1tuy | Intuition\n Describe your first thoughts on how to solve this problem. \nThis problem requires finding the maximum length of a consecutive substring that consis | agarwalmadhur19 | NORMAL | 2023-03-05T06:48:02.522033+00:00 | 2023-03-05T06:48:02.522064+00:00 | 342 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis problem requires finding the maximum length of a consecutive substring that consists of the same character.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe can iterate over the string and maintain two variables - power and currPower. power is used to store the maximum length of consecutive substring seen so far, and currPower is used to store the length of the current consecutive substring being considered.\n\nFor each character in the string, we compare it to the previous character. If they are the same, we increment currPower. If they are different, we update power to be the maximum of power and currPower, and reset currPower to 1.\n\nFinally, we return the maximum of power and currPower, since we need to consider the last consecutive substring seen as well.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe time complexity of this solution is O(n), where n is the length of the input string. This is because we need to iterate over each character in the string exactly once.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity of this solution is O(1), since we only need to store two integer variables regardless of the size of the input string.\n\n# Code\n```\nclass Solution {\n public int maxPower(String s) {\n int power = 1, currPower = 1;\n for(int i = 1 ; i < s.length(); i++)\n {\n if(s.charAt(i) == s.charAt(i - 1))\n {\n currPower++;\n }\n else\n {\n power = Math.max(power, currPower);\n currPower = 1;\n }\n }\n\n return Math.max(power, currPower);\n }\n}\n``` | 2 | 0 | ['String', 'Java'] | 0 |
consecutive-characters | 3 solutions in Python3 ✅✅✅ || Understandable and Beginner-Friendly | 3-solutions-in-python3-understandable-an-rxuf | Code\n\nclass Solution:\n def maxPower(self, s: str) -> int:\n maximum = 0\n count = 0\n for i in range(1, len(s)):\n if s[i | Little_Tiub | NORMAL | 2022-12-15T02:40:14.114193+00:00 | 2022-12-15T02:48:40.144732+00:00 | 29 | false | # Code\n```\nclass Solution:\n def maxPower(self, s: str) -> int:\n maximum = 0\n count = 0\n for i in range(1, len(s)):\n if s[i - 1] == s[i]:\n count += 1\n maximum = max(maximum, count)\n else:\n count = 0\n return maximum + 1\n```\n\n\n\n\n\n```\nclass Solution:\n def maxPower(self, s: str) -> int:\n maximum = 0\n count = 0\n for i in range(1, len(s)):\n if len(set([s[i - 1], s[i]])) == 1:\n count += 1\n maximum = max(maximum, count)\n else:\n count = 0\n return maximum + 1\n```\n\n\n\n\n```\nclass Solution:\n def maxPower(self, s: str) -> int:\n maximum = 0\n count = 0\n for i in range(1, len(s)):\n if len(set([s[i - 1], s[i]])) == 1:\n count += 1\n else:\n count = 0\n maximum = max(maximum, count)\n return maximum + 1\n```\n\n\n\n | 2 | 3 | ['String', 'Python3'] | 0 |
consecutive-characters | PYTHON | consecutive-characters | python-consecutive-characters-by-venkat0-kkw5 | \nclass Solution:\n def maxPower(self, s: str) -> int:\n res=1\n c=1\n for i in range(len(s)-1):\n if s[i]==s[i+1]:\n | Venkat089 | NORMAL | 2022-11-07T07:00:52.830015+00:00 | 2022-11-07T07:00:52.830064+00:00 | 608 | false | ```\nclass Solution:\n def maxPower(self, s: str) -> int:\n res=1\n c=1\n for i in range(len(s)-1):\n if s[i]==s[i+1]:\n c=c+1\n else:\n res=max(res,c)\n c=1\n res=max(res,c)\n return res\n \n\t\t``` | 2 | 0 | ['Python'] | 0 |
consecutive-characters | C# Solution 👌 | c-solution-by-asiashalaldah-j7tj | ```\npublic class Solution {\n public int MaxPower(string s) {\n int n = s.Length;\n int max = Int32.MinValue;\n int count = | AsiaShalaldah | NORMAL | 2022-08-04T09:01:53.067386+00:00 | 2022-08-04T09:01:53.067430+00:00 | 121 | false | ```\npublic class Solution {\n public int MaxPower(string s) {\n int n = s.Length;\n int max = Int32.MinValue;\n int count = 1;\n for (int i = 0; i < n - 1; i++)\n {\n if (s[i] == s[i + 1])\n {\n count++;\n max = Math.Max(max, count);\n }\n else \n {\n count = 1;\n }\n }\n if (max > count)\n return max;\n else\n return count;\n }\n} | 2 | 0 | ['String', 'C#'] | 1 |
consecutive-characters | best approch | best-approch-by-anjali_17-clfi | def maxPower(self, s: str) -> int:\n if(len(s)==1):\n return len(s)\n count=1\n list1=[]\n for i in range (1,len(s)):\n | anjali_17 | NORMAL | 2022-04-29T20:08:07.664084+00:00 | 2022-04-29T20:08:07.664118+00:00 | 28 | false | def maxPower(self, s: str) -> int:\n if(len(s)==1):\n return len(s)\n count=1\n list1=[]\n for i in range (1,len(s)):\n if(s[i]==s[i-1]):\n count+=1\n list1.append(count)\n else:\n list1.append(count)\n count=1\n return max(list1) | 2 | 0 | [] | 0 |
consecutive-characters | Python | 2 Pointer | O(n) time O(1) space | One Pass Solution | python-2-pointer-on-time-o1-space-one-pa-kwwx | This problem is basically asking us to find the longest substring with a given condition - all the characters being the same.\n\nWe can use 2 pointer approach, | infinityhawk | NORMAL | 2022-01-07T17:37:25.677782+00:00 | 2022-01-07T17:37:25.677832+00:00 | 128 | false | This problem is basically asking us to find the longest substring with a given condition - all the characters being the same.\n\nWe can use 2 pointer approach, this one is particularly suited for a straightforward approach using 2 pointers - one that will be "stuck" at the first new character we encounter, the other one advancing from there as long as we find chars with the same value.\n\nTalk is Useless, Let\'s see the code!:\n```\nclass Solution:\n def maxPower(self, s: str) -> int:\n i,j=0,0\n ans=0\n while(j<len(s)):\n if s[i]==s[j]:\n ans=max(ans,j+1-i)\n j+=1\n else:\n i=j\n return(ans)\n``` | 2 | 0 | ['Two Pointers', 'Python'] | 0 |
consecutive-characters | Python || Very Easy Solution || Using groupby | python-very-easy-solution-using-groupby-6i81k | \tclass Solution:\n\t\tdef maxPower(self, s: str) -> int:\n\t\t\tmaxi = 0\n\t\t\tfor i, j in itertools.groupby(s):\n\t\t\t\ttemp = len(list(j))\n\t\t\t\tif temp | naveenrathore | NORMAL | 2021-12-13T14:36:16.361609+00:00 | 2021-12-13T14:36:16.361651+00:00 | 93 | false | \tclass Solution:\n\t\tdef maxPower(self, s: str) -> int:\n\t\t\tmaxi = 0\n\t\t\tfor i, j in itertools.groupby(s):\n\t\t\t\ttemp = len(list(j))\n\t\t\t\tif temp > maxi:\n\t\t\t\t\tmaxi = temp\n\t\t\treturn maxi\n# if you like the solution, Please upvote!! | 2 | 0 | ['Python', 'Python3'] | 0 |
consecutive-characters | ✔️ [Python3] SIMPLE, Explained | python3-simple-explained-by-artod-yra5 | Simply iterate over characters of the string and keep track of the repeating letters. The maximum is our result.\n\nTime: O(n)\nSpase: O(1)\n\nRuntime: 32 ms, f | artod | NORMAL | 2021-12-13T05:12:36.615212+00:00 | 2021-12-13T05:21:38.929496+00:00 | 31 | false | Simply iterate over characters of the string and keep track of the repeating letters. The maximum is our result.\n\nTime: **O(n)**\nSpase: **O(1)**\n\nRuntime: 32 ms, faster than **98.04%** of Python3 online submissions for Consecutive Characters.\nMemory Usage: 14 MB, less than **98.64%** of Python3 online submissions for Consecutive Characters.\n\n```\nclass Solution:\n def maxPower(self, s: str) -> int:\n res = 1\n \n p = 1\n prev = None\n \n for ch in s:\n if ch != prev:\n prev = ch\n p = 1\n else:\n p += 1\n\n res = max(res, p)\n \n return res\n``` | 2 | 4 | ['Python3'] | 0 |
consecutive-characters | Easy C++ Solution | easy-c-solution-by-bogarranganathan1-un8o | class Solution {\npublic:\n int maxPower(string s) {\n int count=1; \n int max=1; \n char a=s[0];\n for(int i=1;imax){\n | bogarranganathan1 | NORMAL | 2021-09-14T06:23:14.438161+00:00 | 2021-09-14T06:23:14.438212+00:00 | 49 | false | class Solution {\npublic:\n int maxPower(string s) {\n int count=1; \n int max=1; \n char a=s[0];\n for(int i=1;i<s.length();i++){\n if(s[i]==a){\n count++;\n if(count>max){\n max=count; \n }\n }\n else{\n a=s[i];\n count=1; \n \n }\n \n \n \n \n }\n return max;\n }\n}; | 2 | 0 | ['String'] | 0 |
consecutive-characters | Simple java solution | simple-java-solution-by-siddhant_1602-yz03 | class Solution {\n\n public int maxPower(String s) {\n if(s.length()==1)\n return 1;\n int i,j=0,k=0;\n for(i=0;i<s.length()- | Siddhant_1602 | NORMAL | 2021-07-23T04:27:44.929232+00:00 | 2022-03-08T06:18:10.941683+00:00 | 52 | false | class Solution {\n\n public int maxPower(String s) {\n if(s.length()==1)\n return 1;\n int i,j=0,k=0;\n for(i=0;i<s.length()-1;i++)\n {\n if(s.charAt(i)==s.charAt(i+1))\n j++;\n else\n {\n j++;\n k=Math.max(j,k);\n j=0;\n }\n }\n j++;\n k=Math.max(j,k);\n return k;\n }\n} | 2 | 0 | [] | 0 |
consecutive-characters | python3 | python3-by-saurabht462-5hcx | ```\nclass Solution:\n def maxPower(self, s: str) -> int:\n if not s:return 0\n c=1\n mc=1\n for i in range(len(s)-1):\n | saurabht462 | NORMAL | 2021-04-21T20:09:00.437242+00:00 | 2021-04-21T20:09:00.437271+00:00 | 78 | false | ```\nclass Solution:\n def maxPower(self, s: str) -> int:\n if not s:return 0\n c=1\n mc=1\n for i in range(len(s)-1):\n if s[i]==s[i+1]:\n c+=1\n else:\n c=1\n mc=max(c,mc)\n return mc | 2 | 0 | [] | 0 |
consecutive-characters | Java || 2-pointer || beats 100% || 1ms || O(s.length()) | java-2-pointer-beats-100-1ms-oslength-by-oxyp | \n public int maxPower(String s) {\n\t\tint omax = 1, ptr1 = 0, ptr2 = 0, len = s.length();\n\t\twhile (ptr1 < len) {\n\n\t\t\twhile (ptr1 < len && s.charAt( | LegendaryCoder | NORMAL | 2021-02-17T21:11:19.116839+00:00 | 2021-02-17T21:11:19.116867+00:00 | 77 | false | \n public int maxPower(String s) {\n\t\tint omax = 1, ptr1 = 0, ptr2 = 0, len = s.length();\n\t\twhile (ptr1 < len) {\n\n\t\t\twhile (ptr1 < len && s.charAt(ptr1) == s.charAt(ptr2))\n\t\t\t\tptr1++;\n\n\t\t\tomax = Math.max(omax, ptr1 - ptr2);\n\t\t\tptr2 = ptr1;\n\t\t}\n\t\treturn omax;\n\t}\n | 2 | 1 | [] | 0 |
consecutive-characters | C++ | easy one pass solution | c-easy-one-pass-solution-by-rv237-b5gg | \nclass Solution {\npublic:\n int maxPower(string s) {\n int mx=0,x=1;\n for(int i=0;i<s.length();++i)\n {\n if(s[i+1]==s[i]) | rv237 | NORMAL | 2020-12-22T08:32:47.879094+00:00 | 2020-12-22T08:32:47.879136+00:00 | 141 | false | ```\nclass Solution {\npublic:\n int maxPower(string s) {\n int mx=0,x=1;\n for(int i=0;i<s.length();++i)\n {\n if(s[i+1]==s[i]) x++;\n else \n {\n mx=max(mx,x);\n x=1;\n }\n \n }\n return mx;\n }\n};\n``` | 2 | 0 | ['C'] | 2 |
consecutive-characters | Python3 one line | python3-one-line-by-bakerston-nwbo | \ndef maxPower(s):\n\treturn max([len(list(g)) for k,g in itertools.groupby(s)])\n | Bakerston | NORMAL | 2020-11-25T21:52:08.287848+00:00 | 2020-11-25T21:52:08.287893+00:00 | 96 | false | ```\ndef maxPower(s):\n\treturn max([len(list(g)) for k,g in itertools.groupby(s)])\n``` | 2 | 1 | [] | 0 |
consecutive-characters | Fast, Easy & Short C++ Solution | fast-easy-short-c-solution-by-ashuruntim-m1fw | ```\n\nclass Solution {\npublic:\n int maxPower(string s) {\n int ans(1), k(0);\n s += \'*\';\n for (int i = 0; i + 1 < s.length(); ++i) | ashuruntimeterror | NORMAL | 2020-11-05T06:59:47.859181+00:00 | 2020-11-05T06:59:47.859254+00:00 | 80 | false | ```\n\nclass Solution {\npublic:\n int maxPower(string s) {\n int ans(1), k(0);\n s += \'*\';\n for (int i = 0; i + 1 < s.length(); ++i)\n if (s[i] != s[i + 1]) {\n ans = max(ans, i + 1 - k);\n k = i + 1;\n }\n return ans;\n }\n}; | 2 | 0 | [] | 0 |
consecutive-characters | Python3 short solution - Consecutive Characters | python3-short-solution-consecutive-chara-sbr2 | \nclass Solution:\n def maxPower(self, s: str) -> int:\n ans, i = 1, 0\n for j in range(1, len(s)):\n if s[j] == s[i]:\n | r0bertz | NORMAL | 2020-11-04T08:09:09.230133+00:00 | 2020-11-04T08:09:09.230177+00:00 | 257 | false | ```\nclass Solution:\n def maxPower(self, s: str) -> int:\n ans, i = 1, 0\n for j in range(1, len(s)):\n if s[j] == s[i]:\n ans = max(ans, j-i+1)\n else:\n i = j\n return ans \n``` | 2 | 0 | ['Python', 'Python3'] | 0 |
consecutive-characters | easy python 3 solution | easy-python-3-solution-by-sadman022-eg5z | \ndef maxPower(self, s: str) -> int:\n\tl = len(s)\n\tp = 0\n\tcount = 1\n\tfor i in range(1, l):\n\t\tif s[i]==s[i-1]:\n\t\t\tcount+=1\n\t\telse:\n\t\t\tp = ma | sadman022 | NORMAL | 2020-11-04T00:29:58.816670+00:00 | 2020-11-04T00:29:58.816707+00:00 | 241 | false | ```\ndef maxPower(self, s: str) -> int:\n\tl = len(s)\n\tp = 0\n\tcount = 1\n\tfor i in range(1, l):\n\t\tif s[i]==s[i-1]:\n\t\t\tcount+=1\n\t\telse:\n\t\t\tp = max(p, count)\n\t\t\tcount = 1\n\treturn max(p, count)\n``` | 2 | 0 | ['Python3'] | 0 |
consecutive-characters | Java Solution Beats 100% | java-solution-beats-100-by-akanshamalik0-0ltu | \n\n public int Power(String s) {\n int i=0;\n int j =0;\n int max = 0;\n while(j < s.length()){\n if(s.charAt(i) == s. | akanshamalik00 | NORMAL | 2020-11-03T19:42:28.779026+00:00 | 2020-11-03T19:42:28.779101+00:00 | 52 | false | ```\n\n public int Power(String s) {\n int i=0;\n int j =0;\n int max = 0;\n while(j < s.length()){\n if(s.charAt(i) == s.charAt(j)){\n if(max < (j-i+1)){\n max = j-i+1;\n }\n j++;\n } \n else{\n i =j;\n }\n }\n \n return max;\n }\n\n``` | 2 | 0 | [] | 0 |
consecutive-characters | [C++/Python] linear scanning with two pointers | cpython-linear-scanning-with-two-pointer-4bib | C++\n\n\nclass Solution {// Linear scanning with two pointers\npublic: // Time/Space: O(N); O(1)\n int maxPower(string s) {\n int ans = 1;\n fo | codedayday | NORMAL | 2020-11-03T17:23:27.024836+00:00 | 2020-11-03T17:33:35.579820+00:00 | 183 | false | C++\n\n```\nclass Solution {// Linear scanning with two pointers\npublic: // Time/Space: O(N); O(1)\n int maxPower(string s) {\n int ans = 1;\n for(int i = 1, prev = 0, n = s.size(); i < n; i++){\n while(i < n && s[i] == s[prev]) i++;\n ans = max(ans, i - prev);\n prev = i;\n }\n return ans;\n }\n};\n```\n\nPython: \n```\ndef maxPower(self, s):\n return max(len(list(b)) for a, b in itertools.groupby(s))\n```\nNotes: playing with itertools.groupby:\n\n```\ns = "leetcode"\nfor a, b in itertools.groupby(s):print(a, " --> ", b, " --> ", list(b))\n```\nOutput:\n```\nl --> <itertools._grouper object at 0x7f6bdab18160> --> [\'l\']\ne --> <itertools._grouper object at 0x7f6bdab12748> --> [\'e\', \'e\']\nt --> <itertools._grouper object at 0x7f6bdab12d30> --> [\'t\']\nc --> <itertools._grouper object at 0x7f6bdab12748> --> [\'c\']\no --> <itertools._grouper object at 0x7f6bdab12d30> --> [\'o\']\nd --> <itertools._grouper object at 0x7f6bdab12748> --> [\'d\']\ne --> <itertools._grouper object at 0x7f6bdab12d30> --> [\'e\']\n``` | 2 | 0 | [] | 0 |
consecutive-characters | Consecutive Characters | Java | 2 Pointers |O(N) | consecutive-characters-java-2-pointers-o-kswk | \nclass Solution {\n public int maxPower(String s) {\n int max = 1;\n int i=0,j=1;\n\t\t\n for(i=0,j=1; j<s.length(); j++){\n | abideenzainuel | NORMAL | 2020-11-03T16:40:07.717727+00:00 | 2020-11-05T03:12:01.008972+00:00 | 215 | false | ```\nclass Solution {\n public int maxPower(String s) {\n int max = 1;\n int i=0,j=1;\n\t\t\n for(i=0,j=1; j<s.length(); j++){\n \n if(s.charAt(i)!=s.charAt(j)){\n max = Math.max(j-i,max);\n i=j;\n }\n }\n return Math.max(j-i,max); \n \n }\n}\n``` | 2 | 0 | ['Java'] | 2 |
consecutive-characters | Rust oneliner | rust-oneliner-by-tianyishi2001-zg4t | rust\npub fn max_power(s: String) -> i32 {\n s.as_bytes()\n .windows(2)\n .fold((1, 1), |(curr, max), win| {\n let curr = if win[0] | tianyishi2001 | NORMAL | 2020-11-03T15:18:56.189314+00:00 | 2020-11-03T15:18:56.189350+00:00 | 69 | false | ```rust\npub fn max_power(s: String) -> i32 {\n s.as_bytes()\n .windows(2)\n .fold((1, 1), |(curr, max), win| {\n let curr = if win[0] == win[1] { curr + 1 } else { 1 };\n (curr, std::cmp::max(curr, max))\n })\n .1\n}\n``` | 2 | 0 | [] | 1 |
consecutive-characters | Python easy solution. Memory: less than 100.00% .faster than 89.00% of Python3 online submissions | python-easy-solution-memory-less-than-10-9cor | \tclass Solution:\n\t\tdef maxPower(self, s: str) -> int:\n\t\t\tif not s:\n\t\t\t\treturn 0\n\t\t\tpower = [0] * len(s)\n\t\t\tprev = s[0]\n\t\t\tpower[0] = 1\ | m-d-f | NORMAL | 2020-11-03T11:27:51.037346+00:00 | 2020-11-03T11:27:51.037388+00:00 | 173 | false | \tclass Solution:\n\t\tdef maxPower(self, s: str) -> int:\n\t\t\tif not s:\n\t\t\t\treturn 0\n\t\t\tpower = [0] * len(s)\n\t\t\tprev = s[0]\n\t\t\tpower[0] = 1\n\t\t\tfor index, char in enumerate(s[1:], 1):\n\t\t\t\tif prev == char:\n\t\t\t\t\tpower[index] = power[index - 1] + 1\n\t\t\t\telse:\n\t\t\t\t\tpower[index] = 1\n\t\t\t\tprev = char\n \n\t\t\treturn max(power) | 2 | 0 | ['Python3'] | 0 |
consecutive-characters | [C++] | Shortest answer | O(n) | c-shortest-answer-on-by-synapse50-nkd6 | \nint maxPower(string s) {\n int i=0,n=s.length()-1,ans=1,c;\n while(i<n){\n c=1;\n while(s[i]==s[i+1])c++,i++; //count how | synapse50 | NORMAL | 2020-11-03T08:59:19.643246+00:00 | 2020-11-03T08:59:19.643280+00:00 | 110 | false | ```\nint maxPower(string s) {\n int i=0,n=s.length()-1,ans=1,c;\n while(i<n){\n c=1;\n while(s[i]==s[i+1])c++,i++; //count how many are same\n ans=max(ans,c); //max check\n i++;\n }\n return ans;\n }\n``` | 2 | 1 | [] | 0 |
consecutive-characters | Java O(n) solution | java-on-solution-by-samspar23-j3wc | ```\nclass Solution {\n public int maxPower(String s) {\n int n = s.length(), ans = 0, count = 1;\n for (int i = 0; i < n; ++i) {\n | samspar23 | NORMAL | 2020-11-03T08:09:43.520331+00:00 | 2020-11-03T08:09:43.520376+00:00 | 237 | false | ```\nclass Solution {\n public int maxPower(String s) {\n int n = s.length(), ans = 0, count = 1;\n for (int i = 0; i < n; ++i) {\n if (i == n - 1 || s.charAt(i) != s.charAt(i + 1)) {\n ans = Math.max(ans, count);\n count = 1; \n }\n else {\n count++;\n }\n }\n return ans;\n }\n} | 2 | 0 | [] | 0 |
consecutive-characters | c++ simple solution | c-simple-solution-by-bibinprakashselvaku-w9iw | C++ simple solution\n This problem is similar to counting consecutive 1\'s.\n In this problem if all the characters are unique, we\'ll have to return 1.\n Also, | bibinprakashselvakumar | NORMAL | 2020-09-20T11:25:51.022486+00:00 | 2020-09-20T11:25:51.022518+00:00 | 78 | false | # **C++ simple solution**\n* This problem is similar to counting consecutive 1\'s.\n* In this problem if all the characters are unique, we\'ll have to return 1.\n* Also, keep the *longestcount* set to 1, so that whenever you get a match with another character, you can increment the *longestcount* and it becomes 2.\n* Keep track of the longest_count in *ans* and return *ans*\n\n```\nclass Solution {\npublic:\n int maxPower(string s) {\n int ans = 1;\n int longest_count = 1;\n for(int i = 1; i < s.length(); i++){\n if(s[i] == s[i-1]){\n longest_count++;\n ans = max(ans, longest_count);\n }\n else{\n longest_count = 1;\n }\n }\n return ans;\n }\n};\n``` | 2 | 0 | [] | 0 |
consecutive-characters | Python 3 beats 93% of Python Users !!! | python-3-beats-93-of-python-users-by-amu-nt4t | \n\nclass Solution:\n def maxPower(self, s: str) -> int:\n out=[]\n count=0\n for i in range(len(s)-1):\n if s[i] == s[i+1]:\ | amulayauppal1999 | NORMAL | 2020-08-18T04:44:28.463452+00:00 | 2020-08-18T04:45:35.232932+00:00 | 222 | false | \n```\nclass Solution:\n def maxPower(self, s: str) -> int:\n out=[]\n count=0\n for i in range(len(s)-1):\n if s[i] == s[i+1]:\n count+=1\n out.append(count)\n else:\n count=0\n if out ==[]:\n return 1\n return max(out)+1\n``` | 2 | 2 | ['Python3'] | 0 |
consecutive-characters | Java O(n) loops | java-on-loops-by-hobiter-iy3u | \npublic int maxPower(String s) {\n if (s == null || s.length() == 0) return 0;\n int res = 0, i = 0, n = s.length();\n while (i < n) {\n | hobiter | NORMAL | 2020-05-31T08:00:11.268381+00:00 | 2020-05-31T08:00:11.268429+00:00 | 167 | false | ```\npublic int maxPower(String s) {\n if (s == null || s.length() == 0) return 0;\n int res = 0, i = 0, n = s.length();\n while (i < n) {\n int r = i;\n while (r + 1 < n && s.charAt(r + 1) == s.charAt(r)) r++;\n res = Math.max(res, r - i + 1);\n i = r + 1;\n }\n return res;\n }\n``` | 2 | 0 | [] | 0 |
consecutive-characters | a few solutions | a-few-solutions-by-claytonjwong-x8rh | Perform a linear scan of the input string s to find the maximum size of the same consecutive characters as the best answer.\n\n---\n\nKotlin\n\nclass Solution { | claytonjwong | NORMAL | 2020-05-18T23:27:06.542443+00:00 | 2021-12-13T22:08:02.755173+00:00 | 88 | false | Perform a linear scan of the input string `s` to find the maximum `size` of the same consecutive characters as the `best` answer.\n\n---\n\n*Kotlin*\n```\nclass Solution {\n fun maxPower(s: String): Int {\n var (best, size) = listOf(1, 1)\n for (i in 1 until s.length)\n if (s[i - 1] == s[i])\n best = Math.max(best, ++size)\n else\n size = 1\n return best\n }\n}\n```\n\n*Javascript*\n```\nlet maxPower = s => {\n let [best, size] = [1, 1];\n for (let i = 1; i < s.length; ++i)\n if (s[i - 1] == s[i])\n best = Math.max(best, ++size);\n else\n size = 1;\n return best;\n};\n```\n\n*Python3*\n```\nclass Solution:\n def maxPower(self, s: str) -> int:\n best, size = 1, 1\n for i in range(1, len(s)):\n if s[i - 1] == s[i]:\n size += 1\n best = max(best, size)\n else:\n size = 1\n return best\n```\n\n*C++*\n```\nclass Solution {\npublic:\n int maxPower(string s) {\n auto [best, size] = make_tuple(1, 1);\n for (auto i{ 1 }; i < s.size(); ++i)\n if (s[i - 1] == s[i])\n best = max(best, ++size);\n else\n size = 1;\n return best;\n }\n};\n``` | 2 | 0 | [] | 0 |
consecutive-characters | [C++] | O(n) | simple logic | detail explanation | c-on-simple-logic-detail-explanation-by-pms80 | logic : take a character as pivot and check next characters\n\nint maxPower(string s) {\n int ma=0,j=0;\n while(1){\n int z=0;char c=s[j | synapse50 | NORMAL | 2020-05-16T16:13:00.749582+00:00 | 2020-05-16T16:13:00.749628+00:00 | 140 | false | logic : take a character as pivot and check next characters\n```\nint maxPower(string s) {\n int ma=0,j=0;\n while(1){\n int z=0;char c=s[j];\n\t\t//if that character is repeating just count it\n while(s[j]==c){\n z++;j++;\n if(j==s.length())break;\n }\n\t\t//check if new count is the maximum or not\n ma=max(ma,z);\n if(j==s.length())break;\n }\n return ma;\n }\n```\nthank you | please upvote | 2 | 1 | [] | 1 |
consecutive-characters | c++ go through each chunk at a time O(n) | c-go-through-each-chunk-at-a-time-on-by-ckmh8 | \nclass Solution {\npublic:\n int maxPower(string s) {\n int n=s.length(),res=0;\n for(int i=0;i<n;){\n int curr=0,start=s[i];\n | apluscs | NORMAL | 2020-05-16T16:08:39.130747+00:00 | 2020-05-16T16:08:39.130794+00:00 | 86 | false | ```\nclass Solution {\npublic:\n int maxPower(string s) {\n int n=s.length(),res=0;\n for(int i=0;i<n;){\n int curr=0,start=s[i];\n while(i<n&&s[i]==start){\n i++;\n curr++;\n }\n res=max(res,curr);\n }\n return res;\n }\n};\n``` | 2 | 0 | [] | 0 |
consecutive-characters | [Python] Simple one pass - O(n) time O(1) memory | python-simple-one-pass-on-time-o1-memory-d6qc | Simple O(n) time O(1) memory python\n\nBasically just have to remember the previous char, current \'same char\' sequence, and the best \'same char\' sequence se | theleetman | NORMAL | 2020-05-16T16:03:14.151196+00:00 | 2020-05-17T06:52:23.544626+00:00 | 223 | false | Simple O(n) time O(1) memory python\n\nBasically just have to remember the previous char, current \'same char\' sequence, and the best \'same char\' sequence seen.\n\n```\n\nclass Solution:\n def maxPower(self, s: str) -> int:\n if not s or len(s) == 0: return 0\n prev, counter, best = None, 0, 1\n for c in s:\n if c == prev:\n counter += 1\n best = max(best, counter)\n else:\n counter = 1\n prev = c\n return best\n``` | 2 | 1 | [] | 2 |
consecutive-characters | Two pointer approach Time Complexity (O(n)) beats 100% | two-pointer-approach-time-complexity-on-f07r9 | IntuitionTwo pointerApproachpointerComplexity
Time complexity:
O(n)
Space complexity:
O(1);
Code | Dhanusubramanir | NORMAL | 2025-04-05T02:03:57.640658+00:00 | 2025-04-05T02:03:57.640658+00:00 | 47 | false | # Intuition
Two pointer
# Approach
pointer
# Complexity
- Time complexity:
O(n)
- Space complexity:
O(1);
# Code
```java []
class Solution {
public int maxPower(String s) {
int i=0,j=1,max=0,count=1;
if(s.length()==1)return 1;
while(j<s.length()){
count=1;
i=j-1;
while(j<s.length()&&s.charAt(j)==s.charAt(j-1)){
count++;
j++;
}
max=Math.max(max,count);
j++;
}
return max;
}
}
``` | 1 | 0 | ['Two Pointers', 'Java'] | 0 |
consecutive-characters | Beats 100% by simple if and else | beats-100-by-simple-if-and-else-by-divya-82jy | Complexity- Time complexity: O(N)- Space complexity: O(1)Code | DivyanshuVortex | NORMAL | 2025-01-30T03:30:54.452378+00:00 | 2025-01-30T03:30:54.452378+00:00 | 173 | false |
# Complexity
# - Time complexity: O(N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
# - Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
int maxPower(string s) {
int ans = 1;
int maxx = 1;
char d;
for(int i = 1; i<s.size() ; i++){
d = s[i];
if(s[i-1] == d){
ans++;
maxx = max(ans,maxx);
}else{
ans = 1;
}
}
return maxx;
}
};
``` | 1 | 0 | ['String', 'C++'] | 0 |
consecutive-characters | Fastest and Easiest java solution | fastest-and-easiest-java-solution-by-bra-4vt4 | Intuitioncompare current and next character if equal increment count else compare with the maxCount and return the maxCount.Approach
We need two variables, 1st | BrajeshPrajapati003 | NORMAL | 2025-01-09T18:26:59.874146+00:00 | 2025-01-09T18:26:59.874146+00:00 | 268 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
compare current and next character if equal increment count else compare with the maxCount and return the maxCount.
# Approach
<!-- Describe your approach to solving the problem. -->
1. We need two variables, 1st to count the count the number of equal characters and 2nd to store the maximum count
2. Iterate over the string, if current and next characters are equal increment count by 1 and compare it with maxCount else make count = 1
3. In case of s.charAt(i) == s.charAt(i+1), if maxCount is lower assign maxCount = count
4. Return maxCount
# Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public int maxPower(String s) {
int count = 1;
int maxCount = 1;
for(int i=0; i<s.length()-1; i++){
if( s.charAt(i) == s.charAt(i+1)){
count++;
if(maxCount < count){
maxCount = count;
}
}else{
count = 1;
}
}
return maxCount;
}
}
``` | 1 | 0 | ['Java'] | 0 |
consecutive-characters | Easy JS solution() | easy-js-solution-by-joelll-g8xg | 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 | Joelll | NORMAL | 2024-11-22T05:04:49.835172+00:00 | 2024-11-22T05:07:38.744170+00:00 | 14 | 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```javascript []\n/**\n * @param {string} s\n * @return {number}\n */\nvar maxPower = function (s) {\n let max = []\n let count = 1;\n for (let i = 0; i <= s.length; i++) {\n s[i] === s[i - 1] ? count++ : (max.push(count), count = 1);\n }\n return Math.max(...max)\n};\n``` | 1 | 0 | ['JavaScript'] | 0 |
consecutive-characters | JS SOLUTION O(N) | js-solution-on-by-joelll-fccc | 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 | Joelll | NORMAL | 2024-11-22T05:00:08.972211+00:00 | 2024-11-22T05:00:08.972247+00:00 | 13 | 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```javascript []\n/**\n * @param {string} s\n * @return {number}\n */\nvar maxPower = function (s) {\n let max = []\n let count = 1;\n for (let i = 1; i <= s.length; i++) {\n if (s[i] === s[i - 1]) {\n count++\n } else {\n max.push(count)\n count = 1\n }\n }\n return Math.max(...max)\n};\n``` | 1 | 0 | ['JavaScript'] | 0 |
consecutive-characters | Simple solution | simple-solution-by-saloydinov-p09e | 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 | saloydinov | NORMAL | 2024-11-08T06:43:22.294329+00:00 | 2024-11-08T06:43:22.294349+00:00 | 281 | 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<!-- 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```python3 []\nclass Solution:\n def maxPower(self, s: str) -> int:\n max_power = 1\n count = 1\n\n i = 1\n while i < len(s):\n if s[i] == s[i - 1]:\n count += 1\n else:\n count = 1\n i += 1\n max_power = max(count, max_power)\n\n return max_power\n``` | 1 | 0 | ['Python3'] | 0 |
consecutive-characters | 🔍Very Easy and Optimal Java Solution! 🚀 | very-easy-and-optimal-java-solution-by-s-pyjs | Intuition\n\n\nThe problem asks for the maximum number of consecutive occurrences of the same character within a given string. To solve this, we can iterate thr | shoaibkhalid65 | NORMAL | 2024-09-16T23:13:00.342166+00:00 | 2024-09-16T23:13:00.342190+00:00 | 249 | false | # Intuition\n\n\nThe problem asks for the maximum number of consecutive occurrences of the same character within a given string. To solve this, we can iterate through the string and keep track of the current character and its consecutive count. If we encounter a different character, we update the maximum count and reset the consecutive count for the new character.\n\n# Approach\nInitialization:\n\nInitialize a variable power to 1 (for the first character) and a variable pow to 0 to store the maximum consecutive count.\nInitialize a variable c (for the previous character) with a space or any value that is not in the given string.\nIteration:\n\nIterate through the string s.\nFor each character c1:\nIf c1 is the same as the previous character c, increment power.\nOtherwise, set power to 1 (for the new character) and update c to c1.\nUpdating Maximum Count:\n\nAfter each iteration, update pow to the maximum value between the current power and the previous pow.\nReturn:\n\nReturn the final value of pow, which represents the maximum number of consecutive occurrences.\n\n# Complexity\n- Time complexity:\n O(n), where n is the length of the string. We iterate through the string once.\n\n- Space complexity:\nO(1) since we are only using a few constant-sized variables.\n\n# Code\n```java []\nclass Solution {\n public int maxPower(String s) {\n int power=1;\n int c=\' \';\n int pow=0;\n for(int i=0;i<s.length();i++){\n char c1=s.charAt(i);\n if(c1==c){\n power++;\n }else\n power=1;\n c=c1;\n pow=Math.max(pow,power);\n \n }\n return pow;\n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
consecutive-characters | Java soln | java-soln-by-shreyagarg63-nf6c | \n# Time complexity: Constant time\n\n# Space complexity: Constant time\n\n# Code\n\nclass Solution {\n public int maxPower(String s) {\n int[] power | ShreyaGarg63 | NORMAL | 2024-07-14T14:05:37.737729+00:00 | 2024-07-14T14:05:37.737770+00:00 | 6 | false | \n# Time complexity: Constant time\n\n# Space complexity: Constant time\n\n# Code\n```\nclass Solution {\n public int maxPower(String s) {\n int[] power = new int[s.length()];\n power[0] = 1;\n int pow=1;\n for (int i = 1; i < s.length(); i++) {\n if(s.charAt(i) == s.charAt(i-1))\n pow++;\n else\n pow =1;\n power[i] = pow; \n }\n pow = Arrays.stream(power).max().getAsInt();\n return pow;\n \n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
consecutive-characters | Beginner understandable || 100% T.C || CPP | beginner-understandable-100-tc-cpp-by-ga-phxu | 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 | Ganesh_ag10 | NORMAL | 2024-07-04T11:54:49.128038+00:00 | 2024-07-04T11:54:49.128061+00:00 | 37 | 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<!-- 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 {\npublic:\n int maxPower(string s) {\n if(s.size()==1) return 1;\n int i=0,j=1;\n int maxi=0;\n int count=1;\n while(j<s.size() && i<j){\n if(s[i]==s[j]){\n while(s[i]==s[j] && j<s.size()){\n j++;\n count++;\n }\n }\n else{\n i=j;\n j=j+1;\n count=1;\n }\n if(count!=0) maxi=max(maxi,count);\n }\n return maxi;\n }\n};\n``` | 1 | 0 | ['C++'] | 1 |
consecutive-characters | 💥 Kadane's Algorithm Made Simple! 🚀 | kadanes-algorithm-made-simple-by-abinaya-tsvl | Power of a String: Finding the Longest Substring with One Unique Character \uD83D\uDD20\uD83D\uDCAA\n\nTo determine the "power" of a string, which is the maximu | Abinayaprakash | NORMAL | 2024-07-03T17:04:07.586207+00:00 | 2024-07-03T17:04:25.030474+00:00 | 7 | false | ### Power of a String: Finding the Longest Substring with One Unique Character \uD83D\uDD20\uD83D\uDCAA\n\nTo determine the "power" of a string, which is the maximum length of a non-empty substring that contains only one unique character, we can use a straightforward approach by iterating through the string and counting consecutive characters.\n\n## Approach\n1. Convert the string into a character array for easy access to individual characters.\n2. Initialize two variables: `c` to count the current sequence length of the same character, and `max` to keep track of the maximum sequence length found.\n3. Iterate through the string from the first to the second-to-last character.\n4. Compare the current character with the next one:\n - If they are the same, increment `c`.\n - If they are different, update `max` if `c` is greater than `max`, then reset `c` to 1.\n5. After the loop, return `max`.\n\nHere is the implementation:\n\n```java\nclass Solution {\n public int maxPower(String s) {\n int c = 1, max = 1;\n char[] ch = s.toCharArray();\n \n for (int i = 0; i < s.length() - 1; i++) {\n if (ch[i] == ch[i + 1]) {\n c++;\n max = Math.max(max, c);\n } else {\n c = 1;\n }\n }\n \n return max;\n }\n}\n```\n\n\n## Complexity\n- **Time complexity:** \\(O(n)\\), where \\(n\\) is the length of the string. We iterate through the string once.\n- **Space complexity:** \\(O(1)\\), since we only use a few extra variables for counting and comparison. The space used for the character array is also \\(O(n)\\), but it is dependent on the input size, not an additional overhead. | 1 | 0 | ['String', 'Java'] | 0 |
consecutive-characters | 🔥*Dry Run*🧑💻 + Efficient Explanation🧠 (Beats 100% solutions)🔥 | dry-run-efficient-explanation-beats-100-wxd9l | Intuition\nThe problem asks to find the maximum length of a substring where all the characters are the same. This involves counting the longest consecutive sequ | its_rush | NORMAL | 2024-06-21T19:13:47.489910+00:00 | 2024-06-21T19:13:47.489964+00:00 | 281 | false | # Intuition\nThe problem asks to find the maximum length of a substring where all the characters are the same. This involves counting the longest consecutive sequence of identical characters.\n\n# Approach\n1. **Initialize Counters**:\n - `count` to track the current length of the substring of identical characters.\n - `maxi` to keep track of the maximum length found so far.\n2. **Iterate Through the String**:\n - For each character in the string, check if it is the same as the previous character.\n - If it is, increment the `count`.\n - If it isn\'t, update `maxi` if the current `count` is greater than `maxi`, then reset `count` to 1.\n3. **Final Check**:\n - After the loop, ensure to update `maxi` one last time in case the longest substring ends at the last character.\n\n# Complexity\n- Time complexity:\n $$O(n)$$ where `n` is the length of the string. We traverse the string once.\n\n- Space complexity:\n $$O(1)$$ as we use a constant amount of extra space.\n\n# Dry Run\nLet\'s dry run the example `s = "abbcccddddeeeeedcba"`:\n\n1. **Initialize**:\n - `count = 1`\n - `maxi = 0`\n\n2. **Iterate**:\n - At `i = 1`, `s[1] == s[0]` (both \'b\'): `count = 2`\n - At `i = 2`, `s[2] != s[1]`: `maxi = 2`, `count = 1`\n - At `i = 3`, `s[3] == s[2]` (both \'c\'): `count = 2`\n - At `i = 4`, `s[4] == s[3]` (both \'c\'): `count = 3`\n - At `i = 5`, `s[5] != s[4]`: `maxi = 3`, `count = 1`\n - At `i = 6`, `s[6] == s[5]` (both \'d\'): `count = 2`\n - Continue this process...\n - Final `maxi = 5` for substring "eeeee".\n\nThe dry run confirms that the function correctly calculates the maximum power of consecutive identical characters in the string.\n# Code\n```\nclass Solution {\npublic:\n int maxPower(string s) {\n if(s.length() == 1) { \n return 1;\n }\n int count = 1 ; \n int maxi = 0;\n for(int i = 1 ; i < s.length(); i++){\n if(s[i]==s[i-1]){\n count++ ; \n }\n else{\n maxi = max(count , maxi );\n count = 1;\n }\n }\n return (count < maxi ) ? maxi : count ;\n }\n};\n``` | 1 | 0 | ['String', 'C++'] | 0 |
consecutive-characters | Easy to understand | easy-to-understand-by-yogesh-kumar-singh-2ver | \n# Code\n\nclass Solution {\n public int maxPower(String s) {\n // if (s.length()==1) {\n // return 1;\n // }\n int current= | Yogesh-Kumar-Singh | NORMAL | 2024-06-21T04:59:10.496041+00:00 | 2024-06-21T04:59:10.496076+00:00 | 13 | false | \n# Code\n```\nclass Solution {\n public int maxPower(String s) {\n // if (s.length()==1) {\n // return 1;\n // }\n int current=1;\n int max=1;\n for (int i=1;i<s.length();i++) {\n if (s.charAt(i-1)!=s.charAt(i)) {\n current=1;\n }\n else {\n current+=1;\n }\n max=Math.max(current,max);\n // if (max<current) {\n // max=current;\n // }\n }\n return max;\n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
consecutive-characters | [Java] Easy 100% solution | java-easy-100-solution-by-ytchouar-l75f | java\nclass Solution {\n public int maxPower(final String s) {\n int max = 1, count = 1;\n\n for(int i = 1; i < s.length(); ++i) {\n | YTchouar | NORMAL | 2024-05-08T07:38:02.837610+00:00 | 2024-05-08T07:38:02.837634+00:00 | 422 | false | ```java\nclass Solution {\n public int maxPower(final String s) {\n int max = 1, count = 1;\n\n for(int i = 1; i < s.length(); ++i) {\n if(s.charAt(i - 1) == s.charAt(i))\n count++;\n else\n count = 1;\n\n max = Math.max(max, count);\n }\n\n return max;\n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
consecutive-characters | Simple Python and Java solution | Easy to understand | simple-python-and-java-solution-easy-to-4doyq | Intuition\n Describe your first thoughts on how to solve this problem. \nThis problem aims to find the maximum consecutive occurrences of any character within a | Pushkar91949 | NORMAL | 2024-04-28T04:20:07.318512+00:00 | 2024-04-28T04:20:07.318530+00:00 | 80 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis problem aims to find the maximum consecutive occurrences of any character within a given string. Essentially, we need to identify continuous sequences of the same character and determine the length of the longest such sequence.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize variables prev, count, and maxcount to track the previous character, count of consecutive occurrences, and the maximum count encountered so far, respectively. \n2. Iterate through the string, starting from the second character. \n3. For each character at index i, check if it matches the previous character (prev). If it does, increment the count variable to represent consecutive occurrences. If it doesn\'t match, update prev to the current character and reset count to 1. \n4. During each iteration, update maxcount to store the maximum value between the current count and the existing maxcount. \n5. Return maxcount, which represents the maximum consecutive occurrences of any character within the input string.\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```Python []\nclass Solution:\n def maxPower(self, s: str) -> int:\n prev = s[0]\n count = 1\n maxcount = 1\n for i in range(1,len(s)):\n if s[i] == prev:\n count += 1\n else:\n prev = s[i]\n count = 1\n maxcount = max(count,maxcount)\n return maxcount\n```\n```Java []\nclass Solution {\n public int maxPower(String s) {\n int prev = s.charAt(0);\n int count = 1;\n int maxcount = 1;\n\n for (int i = 1; i < s.length(); i++){\n if (s.charAt(i) == prev){\n count++;\n }\n else{\n prev = s.charAt(i);\n count = 1;\n }\n maxcount = Math.max(maxcount,count);\n }\n return maxcount;\n }\n}\n``` | 1 | 0 | ['String', 'Python', 'Java', 'Python3'] | 0 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | [C++/Python/Java] Count "AAA" and "BBB" | cpythonjava-count-aaa-and-bbb-by-lokeshs-2kdn | Count "AAA" and "BBB" and compare them\n\nYou can only remove an A that is surrounded by two other A\'s.\nSo if you keep on removing valid A\'s, then the valid | lokeshsk1 | NORMAL | 2021-10-16T16:00:35.300079+00:00 | 2022-10-21T16:16:21.469720+00:00 | 17,475 | false | Count "AAA" and "BBB" and compare them\n\nYou can only remove an A that is surrounded by two other A\'s.\nSo if you keep on removing valid A\'s, then the valid B\'s are not affected. Same goes for all valid B\'s.\nYou just count all the valid A\'s and valid B\'s. At each turn, Alice removes a valid A and the count of all valid A\'s (countA) decreases by one. Similarly after Bob\'s turn, countB decreases by one.\nSo if countA > countB, then a turn comes when Bob has no valid B\'s to remove. So he loses and Alice wins. You return TRUE.\nElse if countA == countB, then at some point Alice has no valid A\'s to remove, So Bob wins and you return FALSE.\nElse (countA < countB), then at some point Alice has no valid A\'s to remove. So Bob wins and you return FALSE.\n\nIn either case, if countA <= countB, then you return FALSE.\nSo just return countA > countB.\n\nPS: There\'s no optimal play here. No matter what valid color a player removes at each turn, it has no effect on the outcome.\nExplanation credits : @Zombiesalad \n\n**Python:**\n```\nclass Solution:\n def winnerOfGame(self, s: str) -> bool:\n \n a = b = 0\n \n for i in range(1,len(s)-1):\n if s[i-1] == s[i] == s[i+1]:\n if s[i] == \'A\':\n a += 1\n else:\n b += 1\n \n return a>b\n```\n\n\n**C++:**\n```\nclass Solution {\npublic:\n bool winnerOfGame(string s) {\n \n int a = 0, b = 0;\n \n for(int i=1; i<s.size()-1; i++){\n if(s[i-1] == s[i] && s[i] == s[i+1]){\n if(s[i] == \'A\')\n a++;\n else\n b++;\n\t\t\t}\n } \n \n return a>b;\n }\n};\n```\n\n\n**Java:**\n```\nclass Solution {\n public boolean winnerOfGame(String s) {\n \n int a = 0, b = 0;\n \n for(int i=1; i<s.length()-1; i++){\n if(s.charAt(i) == s.charAt(i-1) && s.charAt(i) == s.charAt(i+1)){\n if(s.charAt(i) == \'A\')\n a++;\n else\n b++;\n\t\t\t}\n }\n \n return a>b;\n }\n}\n```\n | 161 | 3 | ['C', 'Counting', 'Python', 'Java', 'Python3'] | 25 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | ✅96.44%🔥Easy Solution🔥Game Theory & Greedy | 9644easy-solutiongame-theory-greedy-by-m-w9mr | Problem Explanation:\n#### In this problem, you are given a string colors representing a line of colored pieces, where each piece is either colored \'A\' or \'B | MrAke | NORMAL | 2023-10-02T00:22:43.133316+00:00 | 2023-10-02T00:22:43.133336+00:00 | 21,147 | false | # Problem Explanation:\n#### In this problem, you are given a string colors representing a line of colored pieces, where each piece is either colored \'A\' or \'B\'. Alice and Bob are playing a game where they take alternating turns removing pieces from the line. Alice goes first, and they have certain rules for removing pieces:\n\n- Alice can only remove a piece colored \'A\' if both its neighbors are also colored \'A\'. She cannot remove pieces that are colored \'B\'.\n- Bob can only remove a piece colored \'B\' if both its neighbors are also colored \'B\'. He cannot remove pieces that are colored \'A\'.\n- They cannot remove pieces from the edge of the line.\n\n#### The game continues until one of the players cannot make a move, and in that case, the other player wins.\n\n#### You need to determine, assuming both Alice and Bob play optimally, whether Alice can win the game or not.\n---\n# Solution Approach:\n#### The given solution approach uses a Counter object to count how many valid moves Alice can make and how many valid moves Bob can make. It iterates through the input string colors to count these moves.\n\n- The collections.Counter() is used to count the occurrences of characters in the string.\n- The groupby(colors) function groups consecutive characters in the string.\n- It iterates through the groups and counts how many \'A\'s and \'B\'s can be removed according to the game rules. Specifically, it counts the number of \'A\'s and \'B\'s that have at least two neighbors of the same color.\n- Finally, it compares the counts of valid moves for Alice (\'A\') and Bob (\'B\'). If Alice has more valid moves, it returns True; otherwise, it returns False.\n\n#### The idea is that if Alice can make more valid moves than Bob, she has a winning strategy because she will eventually force Bob into a position where he can\'t make a move.\n\n#### This solution works under the assumption that both players play optimally and is a good approach to solve the problem efficiently.\n\n\n---\n# Code\n```python []\nclass Solution:\n def winnerOfGame(self, colors: str) -> bool:\n c = collections.Counter()\n for x, t in groupby(colors):\n c[x] += max(len(list(t)) - 2, 0)\n\n if c[\'A\'] > c[\'B\']:\n return True\n return False\n```\n```C# []\npublic class Solution {\n public bool WinnerOfGame(string colors) {\n int countA = 0;\n int countB = 0;\n \n for (int i = 0; i < colors.Length; i++) {\n char x = colors[i];\n int count = 0;\n \n while (i < colors.Length && colors[i] == x) {\n i++;\n count++;\n }\n \n if (x == \'A\') {\n countA += Math.Max(count - 2, 0);\n } else if (x == \'B\') {\n countB += Math.Max(count - 2, 0);\n }\n \n i--;\n }\n\n return countA > countB;\n }\n}\n\n```\n```C++ []\nclass Solution {\npublic:\n bool winnerOfGame(string colors) {\n map<char, int> c;\n for (auto it = colors.begin(); it != colors.end(); ) {\n char x = *it;\n auto t = it;\n while (t != colors.end() && *t == x) {\n t++;\n }\n c[x] += max(int(distance(it, t) - 2), 0);\n it = t;\n }\n\n if (c[\'A\'] > c[\'B\']) {\n return true;\n }\n return false;\n }\n};\n\n```\n```C []\nbool winnerOfGame(char *colors) {\n int len = strlen(colors);\n int countA = 0, countB = 0;\n\n for (int i = 0; i < len; i++) {\n char x = colors[i];\n int count = 0;\n\n while (i < len && colors[i] == x) {\n i++;\n count++;\n }\n\n if (x == \'A\') {\n countA += (count - 2 > 0) ? count - 2 : 0;\n } else if (x == \'B\') {\n countB += (count - 2 > 0) ? count - 2 : 0;\n }\n\n i--; \n }\n\n return countA > countB;\n}\n```\n```Java []\npublic class Solution {\n public boolean winnerOfGame(String colors) {\n Map<Character, Integer> c = new HashMap<>();\n c.put(\'A\', 0);\n c.put(\'B\', 0);\n \n for (int i = 0; i < colors.length(); i++) {\n char x = colors.charAt(i);\n int count = 0;\n \n while (i < colors.length() && colors.charAt(i) == x) {\n i++;\n count++;\n }\n \n c.put(x, c.get(x) + Math.max(count - 2, 0));\n i--;\n }\n\n return c.get(\'A\') > c.get(\'B\');\n }\n}\n\n```\n```javascript []\nvar winnerOfGame = function(colors) {\n let countA = 0;\n let countB = 0;\n\n for (let i = 0; i < colors.length; i++) {\n const x = colors[i];\n let count = 0;\n\n while (i < colors.length && colors[i] === x) {\n i++;\n count++;\n }\n\n if (x === \'A\') {\n countA += Math.max(count - 2, 0);\n } else if (x === \'B\') {\n countB += Math.max(count - 2, 0);\n }\n\n i--;\n }\n\n return countA > countB;\n};\n```\n```Typescript []\nfunction winnerOfGame(colors: string): boolean {\n let countA = 0;\n let countB = 0;\n\n for (let i = 0; i < colors.length; i++) {\n const x = colors[i];\n let count = 0;\n\n while (i < colors.length && colors[i] === x) {\n i++;\n count++;\n }\n\n if (x === \'A\') {\n countA += Math.max(count - 2, 0);\n } else if (x === \'B\') {\n countB += Math.max(count - 2, 0);\n }\n\n i--;\n }\n\n return countA > countB;\n}\n\n```\n```Go []\nfunc winnerOfGame(colors string) bool {\n countA := 0\n countB := 0\n\n for i := 0; i < len(colors); i++ {\n x := colors[i]\n count := 0\n\n for i < len(colors) && colors[i] == x {\n i++\n count++\n }\n\n if x == \'A\' {\n countA += max(count-2, 0)\n } else if x == \'B\' {\n countB += max(count-2, 0)\n }\n\n i--\n }\n\n return countA > countB\n}\n\nfunc max(a, b int) int {\n if a > b {\n return a\n }\n return b\n}\n\n```\n\n\n | 132 | 1 | ['Greedy', 'C', 'Game Theory', 'C++', 'Java', 'Go', 'TypeScript', 'Python3', 'JavaScript', 'C#'] | 14 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | 🚀Count Solution || Explained Intuition || Commented Code🚀 | count-solution-explained-intuition-comme-lx2a | Problem Description\nIn brief, The problem involves a game between Alice and Bob where they take turns removing pieces from a line of A and B colored pieces. Al | MohamedMamdouh20 | NORMAL | 2023-10-02T00:25:03.996106+00:00 | 2023-10-02T01:12:13.322176+00:00 | 6,797 | false | # Problem Description\nIn brief, The problem involves a **game** between `Alice` and `Bob` where they take **turns** **removing** pieces from a line of `A` and `B` colored pieces. `Alice` can only remove `A` pieces if **both** neighbors are also `A`, and `Bob` can only remove `B` pieces if **both** neighbors are `B`. The game **starts** with `Alice` moving **first**. If a player **can\'t** make a move on **their turn**, they **lose**. The task is to determine if `Alice` can win the game assuming **optimal** play.\n\n- **Constraints:**\n - `1 <= colors.length <= 10e5`\n - `colors consists of only the letters A and B`\n - Player only remove piece if its neighbors are the **same** like it\n - Who **can\'t** play in its turn loses\n\n\n---\n\n\n\n# Intuition\nHi there\uD83D\uDE03,\n\nToday\'s problem may seem tough first but it is **very easy** once you **understand** some observations. \uD83D\uDE80\n\nIn our today\'s problem, We have **two** of our friends `Alice` and `Bob` and they are playing some game (a Weird game\uD83D\uDE02). `Alice` only can **remove** piece with letter `A` and its **neighbors** have also letter `A` and `Bob` only can **remove** piece with letter `B` and its **neighbors** have also letter `B`. `Alice` **starts** first and the player who **can\'t** move any more pieces **lose**. \uD83E\uDD14\n\nFrom the description, It seems that we will have to do **many** calculations and go through **many** steps but **actually we won\'t**.\uD83E\uDD29\nLet\'s see some **exapmles**.\n\n```\nEX1: colors = BBAAABBA\n \nAlice\'s Turn -> removes BBA`A`ABBA\ncolors = BBAABBA\n \nBob\'s Turn -> can\'t remove any piece\nAlice wins \n```\n```\nEX2: colors = ABBBABAAAB\n\nAlice\'s Turn -> removes ABBBABA`A`AB\ncolors = ABBBABAAB\n \nBob\'s Turn -> removes AB`B`BABAAB\ncolors = ABBABAAB\n\nAlice\'s Turn -> can\'t remove any piece\nBob wins \n```\n\nI think this is enough for the problem.\nFrom these two examples we can see two important **observations**:\n\n**First** observation, If someone **removed** a piece from its place (Let\'s say Alice) It won\'t give the other player **more** moves to help him play more.\uD83E\uDD14\nEx -> `BBAAABB` -> Alice plays -> `BBAABB` -> Bob loses\nWe can see that from the **begining** of the game, Bob doesn\'t have any pieces to move and **Alice\'s move** didn\'t give hime extra moves like it didn\'t make the string `BBBB`.\uD83E\uDD2F\n\n**Second** observation, From first observation we can **conclude** and I **mentioned** it in the first one also, Every player have **dedicated** number of moves in the **begining** of the game and it **can\'t** be increased or decreased.\uD83D\uDE14\n\nFrom these **two observations** the solution become **much easier**. Only **count** the moves for each player and if Alice has more moves then she wins and if Bob has more or equal moves then he wins.\uD83D\uDE80\uD83D\uDE80\n\nBut Why the last line?\uD83E\uDD2F\uD83E\uDD14\nLets assume that we are playing and see in each turn if the current player has no more moves who wins.\n\n| Turn | Alice\'s Moves | Bob\'s Moves | Who wins | \n| :--- | :---: | :---: | :---: |\n| Alice\'s Turn | 0 | 0 or more |Bob |\n| Bob\'s Turn | 1 or more | 0 |Alice |\n| Alice\'s Turn | 1 | 1 or more | Bob |\n| Bob\'s Turn | 2 or more | 1 |Alice |\n\nAnd we can continue further, But the point that if Alice has more moves then she wins and if Bob has more or equal moves then he wins.\n\nAnd this is the solution for our today problem I hope that you understood it\uD83D\uDE80\uD83D\uDE80\n\n\n---\n\n\n# Approach\n1. **Initialize** scores for Alice and Bob (`aliceScore` and `bobScore`) to `zero`.\n2. **Iterate** through the colors (**excluding** edge pieces) using a loop. For **each** piece in the iteration:\n - Check if it\'s `A` and its **neighboring** pieces are also `A`.\n - If **yes**, **increment** `aliceScore` as Alice can remove the current piece.\n - Check if it\'s `B` and its **neighboring** pieces are also `B`.\n - If **yes**, **increment** `bobScore` as Bob can remove the current piece.\n3. Return **true** if Alice\'s score is **greater** than Bob\'s score; otherwise, return **false**.\n\n# Complexity\n- **Time complexity:** $$O(N)$$\nSince we are iterating over all the pieaces then it is **linear** complexity which is `O(N)`.\n- **Space complexity:** $$O(1)$$\nSince we are only storing couple of **constant** variables then complexity is `O(1)`.\n\n---\n\n\n\n# Code\n```C++ []\nclass Solution {\npublic:\n bool winnerOfGame(string colors) {\n int aliceScore = 0, bobScore = 0; // Initialize scores for Alice and Bob\n\n // Iterate through the colors, excluding the edge pieces\n for (int i = 1; i < colors.size() - 1; i++) {\n char currentColor = colors[i];\n char prevColor = colors[i - 1];\n char nextColor = colors[i + 1];\n\n // Check if Alice can make a move here\n if (currentColor == \'A\' && prevColor == \'A\' && nextColor == \'A\')\n aliceScore++; // Alice can remove \'A\'\n\n // Check if Bob can make a move here\n else if (currentColor == \'B\' && prevColor == \'B\' && nextColor == \'B\')\n bobScore++; // Bob can remove \'B\'\n }\n\n // Determine the winner based on the scores\n return aliceScore > bobScore;\n }\n};\n```\n```Java []\npublic class Solution {\n public boolean winnerOfGame(String colors) {\n int aliceScore = 0;\n int bobScore = 0;\n\n // Iterate through the colors, excluding the edge pieces\n for (int i = 1; i < colors.length() - 1; i++) {\n char currentColor = colors.charAt(i);\n char prevColor = colors.charAt(i - 1);\n char nextColor = colors.charAt(i + 1);\n\n // Check if Alice can make a move here\n if (currentColor == \'A\' && prevColor == \'A\' && nextColor == \'A\')\n aliceScore++; // Alice can remove \'A\'\n\n // Check if Bob can make a move here\n else if (currentColor == \'B\' && prevColor == \'B\' && nextColor == \'B\')\n bobScore++; // Bob can remove \'B\'\n }\n\n // Determine the winner based on the scores\n return aliceScore > bobScore;\n }\n}\n```\n```Python []\nclass Solution:\n def winnerOfGame(self, colors: str) -> bool:\n alice_score = 0\n bob_score = 0\n\n # Iterate through the colors, excluding the edge pieces\n for i in range(1, len(colors) - 1):\n current_color = colors[i]\n prev_color = colors[i - 1]\n next_color = colors[i + 1]\n\n # Check if Alice can make a move here\n if current_color == \'A\' and prev_color == \'A\' and next_color == \'A\':\n alice_score += 1 # Alice can remove \'A\'\n\n # Check if Bob can make a move here\n elif current_color == \'B\' and prev_color == \'B\' and next_color == \'B\':\n bob_score += 1 # Bob can remove \'B\'\n\n # Determine the winner based on the scores\n return alice_score > bob_score\n```\n\n\n\n | 103 | 1 | ['String', 'Python', 'C++', 'Java', 'Python3'] | 12 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | ✅ 99.81% 2-Approaches RegEx & Iterative Count | 9981-2-approaches-regex-iterative-count-r8ys2 | Interview Guide: "Remove Colored Pieces if Both Neighbors are the Same Color" Problem\n\n## Problem Understanding\n\nIn the "Remove Colored Pieces if Both Neigh | vanAmsen | NORMAL | 2023-10-02T00:42:16.352059+00:00 | 2023-10-02T02:56:31.550817+00:00 | 5,582 | false | # Interview Guide: "Remove Colored Pieces if Both Neighbors are the Same Color" Problem\n\n## Problem Understanding\n\nIn the "Remove Colored Pieces if Both Neighbors are the Same Color" problem, you are given a string `colors` where each piece is colored either by \'A\' or by \'B\'. Alice and Bob play a game where they take alternating turns removing pieces from the line. Alice can only remove a piece colored \'A\' if both its neighbors are also colored \'A\' and vice versa for Bob with color \'B\'. Alice moves first, and if a player cannot make a move on their turn, that player loses. The goal is to determine if Alice can win the game.\n\n## Key Points to Consider\n\n### 1. Understand the Constraints\n\nThe length of the string `colors` can be up to $$10^5$$, indicating the need for a solution with a time complexity better than $$O(n^2)$$.\n\n### 2. Multiple Approaches\n\nThere are several methods to solve this problem:\n\n - Regular Expressions (RegEx)\n - Iterative Count\n \nEach method has its own nuances, and understanding the logic behind each can provide multiple tools to tackle similar problems.\n\n### 3. Patterns are Key\n\nIn this problem, the solution heavily relies on recognizing patterns within the string. Identifying sequences of \'A\'s and \'B\'s and understanding how they influence the game\'s outcome is crucial.\n\n### 4. Explain Your Thought Process\n\nArticulate your reasoning behind each approach and the steps involved. This showcases clarity of thought and can help in discussing the problem\'s nuances.\n\n## Conclusion\n\nThe "Remove Colored Pieces if Both Neighbors are the Same Color" problem is a fascinating blend of string manipulation and game theory. By employing pattern recognition and iterative techniques, one can efficiently determine the game\'s outcome. Discussing and demonstrating multiple approaches reflects a comprehensive understanding and adaptability to problem-solving.\n\n---\n\n# Live Coding & Explaining 2 Approaches\nhttps://youtu.be/e3qHIM0byhc?si=td8fkcbWHzQ4x39-\n\n# Approach: Iterative Count\n\nThe iterative count approach involves traversing the `colors` string and counting consecutive \'A\'s and \'B\'s.\n\n## Key Data Structures:\n- **Counters**: Utilized to keep track of consecutive occurrences.\n\n## Enhanced Breakdown:\n\n1. **Initialization**:\n - Initialize counters for Alice\'s and Bob\'s plays. Start a count for consecutive colors.\n \n2. **Iterate through the String**:\n - Traverse the `colors` string. For every character, check if it\'s the same as the previous one to identify consecutive sequences.\n - When the sequence breaks or changes, calculate plays for Alice or Bob based on the color of the sequence.\n \n3. **Handle the Last Segment**:\n - After iterating, handle the last segment of colors and calculate plays for Alice or Bob as needed.\n \n4. **Determine the Winner**:\n - Compare the number of plays for both players. If Alice has more plays, she wins.\n\n## Complexity:\n\n**Time Complexity**: \n- The solution requires a single traversal of the `colors` string, leading to a time complexity of $$O(n)$$, where $$n$$ is the length of the string.\n\n**Space Complexity**: \n- We only utilize a few integer counters to keep track of the sequences and plays, so the space complexity is $$O(1)$$.\n\n# Code Iterative Count\n``` Python []\nclass Solution:\n def winnerOfGame(self, colors: str) -> bool:\n alice_plays, bob_plays = 0, 0\n count = 1 \n \n for i in range(1, len(colors)):\n if colors[i] == colors[i - 1]:\n count += 1\n else:\n if colors[i - 1] == \'A\':\n alice_plays += max(0, count - 2)\n else:\n bob_plays += max(0, count - 2)\n count = 1\n \n if colors[-1] == \'A\':\n alice_plays += max(0, count - 2)\n else:\n bob_plays += max(0, count - 2)\n \n return alice_plays > bob_plays\n```\n``` Rust []\nimpl Solution {\n pub fn winner_of_game(colors: String) -> bool {\n let mut alice_plays = 0;\n let mut bob_plays = 0;\n let mut count = 0;\n let chars: Vec<char> = colors.chars().collect();\n \n for i in 1..chars.len() {\n if chars[i] == chars[i - 1] {\n count += 1;\n } else {\n if chars[i - 1] == \'A\' {\n alice_plays += (count - 1).max(0);\n } else {\n bob_plays += (count - 1).max(0);\n }\n count = 0;\n }\n }\n\n if chars[chars.len() - 1] == \'A\' {\n alice_plays += (count - 1).max(0);\n } else {\n bob_plays += (count - 1).max(0);\n }\n \n alice_plays > bob_plays\n }\n}\n```\n``` Go []\npackage main\n\nfunc winnerOfGame(colors string) bool {\n alice_plays, bob_plays, count := 0, 0, 0\n \n for i := 1; i < len(colors); i++ {\n if colors[i] == colors[i-1] {\n count++\n } else {\n if colors[i-1] == \'A\' {\n alice_plays += max(0, count - 1)\n } else {\n bob_plays += max(0, count - 1)\n }\n count = 0\n }\n }\n\n if colors[len(colors)-1] == \'A\' {\n alice_plays += max(0, count - 1)\n } else {\n bob_plays += max(0, count - 1)\n }\n \n return alice_plays > bob_plays\n}\n\nfunc max(a, b int) int {\n if a > b {\n return a\n }\n return b\n}\n```\n``` C++ []\nclass Solution {\npublic:\n bool winnerOfGame(std::string colors) {\n int alice_plays = 0, bob_plays = 0, count = 0;\n \n for (int i = 1; i < colors.size(); i++) {\n if (colors[i] == colors[i - 1]) {\n count++;\n } else {\n if (colors[i - 1] == \'A\') {\n alice_plays += std::max(0, count - 1);\n } else {\n bob_plays += std::max(0, count - 1);\n }\n count = 0;\n }\n }\n\n if (colors.back() == \'A\') {\n alice_plays += std::max(0, count - 1);\n } else {\n bob_plays += std::max(0, count - 1);\n }\n \n return alice_plays > bob_plays;\n }\n};\n```\n``` Java []\npublic class Solution {\n public boolean winnerOfGame(String colors) {\n int alice_plays = 0, bob_plays = 0, count = 0;\n \n for (int i = 1; i < colors.length(); i++) {\n if (colors.charAt(i) == colors.charAt(i - 1)) {\n count++;\n } else {\n if (colors.charAt(i - 1) == \'A\') {\n alice_plays += Math.max(0, count - 1);\n } else {\n bob_plays += Math.max(0, count - 1);\n }\n count = 0;\n }\n }\n\n if (colors.charAt(colors.length() - 1) == \'A\') {\n alice_plays += Math.max(0, count - 1);\n } else {\n bob_plays += Math.max(0, count - 1);\n }\n \n return alice_plays > bob_plays;\n }\n}\n```\n``` PHP []\nclass Solution {\n function winnerOfGame($colors) {\n $alice_plays = 0; $bob_plays = 0; $count = 0;\n \n for ($i = 1; $i < strlen($colors); $i++) {\n if ($colors[$i] == $colors[$i - 1]) {\n $count++;\n } else {\n if ($colors[$i - 1] == \'A\') {\n $alice_plays += max(0, $count - 1);\n } else {\n $bob_plays += max(0, $count - 1);\n }\n $count = 0;\n }\n }\n\n if ($colors[strlen($colors) - 1] == \'A\') {\n $alice_plays += max(0, $count - 1);\n } else {\n $bob_plays += max(0, $count - 1);\n }\n \n return $alice_plays > $bob_plays;\n }\n}\n```\n``` JavaScript []\n/**\n * @param {string} colors\n * @return {boolean}\n */\nvar winnerOfGame = function(colors) {\n let alice_plays = 0, bob_plays = 0, count = 0;\n \n for (let i = 1; i < colors.length; i++) {\n if (colors[i] == colors[i - 1]) {\n count++;\n } else {\n if (colors[i - 1] == \'A\') {\n alice_plays += Math.max(0, count - 1);\n } else {\n bob_plays += Math.max(0, count - 1);\n }\n count = 0;\n }\n }\n\n if (colors.charAt(colors.length - 1) == \'A\') {\n alice_plays += Math.max(0, count - 1);\n } else {\n bob_plays += Math.max(0, count - 1);\n }\n \n return alice_plays > bob_plays;\n }\n```\n``` C# []\npublic class Solution {\n public bool WinnerOfGame(string colors) {\n int alice_plays = 0, bob_plays = 0, count = 0;\n \n for (int i = 1; i < colors.Length; i++) {\n if (colors[i] == colors[i - 1]) {\n count++;\n } else {\n if (colors[i - 1] == \'A\') {\n alice_plays += Math.Max(0, count - 1);\n } else {\n bob_plays += Math.Max(0, count - 1);\n }\n count = 0;\n }\n }\n\n if (colors[colors.Length - 1] == \'A\') {\n alice_plays += Math.Max(0, count - 1);\n } else {\n bob_plays += Math.Max(0, count - 1);\n }\n \n return alice_plays > bob_plays;\n }\n}\n```\n\n# Approach: RegEx\n\nTo solve this problem using RegEx, we search for patterns within the `colors` string that match consecutive \'A\'s or \'B\'s of length 3 or more.\n\n## Key Data Structures:\n- **Regular Expressions**: Utilized to identify patterns within the string.\n\n## Enhanced Breakdown:\n\n1. **Identify Alice\'s Plays**:\n - Use regular expressions to find all matches of \'A\'s of length 3 or more.\n - For each match, calculate how many times Alice can play (length of match - 2) and aggregate them.\n \n2. **Identify Bob\'s Plays**:\n - Similarly, use regular expressions to find all matches of \'B\'s of length 3 or more.\n - For each match, calculate how many times Bob can play and aggregate them.\n \n3. **Determine the Winner**:\n - Compare the number of plays for Alice and Bob. If Alice has more plays, she wins.\n\n## Complexity:\n\n**Time Complexity**: \n- Regular expressions generally operate in $$O(n)$$ time complexity for simple patterns. However, for complex patterns and longer strings, the time complexity can be worse. In our case, since the patterns are straightforward (repeated \'A\'s or \'B\'s), the time complexity is roughly $$O(n)$$ for each RegEx search, where $$n$$ is the length of the string. Overall, considering two searches (one for \'A\'s and one for \'B\'s), the time complexity remains $$O(n)$$.\n\n**Space Complexity**: \n- The space complexity is mainly dependent on the number of matches found by the RegEx search. In the worst case, if every alternate character is an \'A\' or \'B\', the number of matches would be $$n/3$$. However, these matches are processed sequentially, and we only store the count, making the space complexity $$O(1)$$.\n\n# Code RegEx \n``` Python []\nclass Solution:\n def winnerOfGame(self, colors: str) -> bool:\n alice_plays = sum(len(match.group()) - 2 for match in re.finditer(r\'A{3,}\', colors))\n \n bob_plays = sum(len(match.group()) - 2 for match in re.finditer(r\'B{3,}\', colors))\n \n return alice_plays > bob_plays\n```\n\n## Performance\n| Language | Time (ms) | Space (MB) | Approach |\n|------------|-----------|------------|----------|\n| Rust | 6 | 2.7 | Counter |\n| Go | 13 | 6.5 | Counter |\n| Java | 14 | 44.4 | Counter |\n| C++ | 34 | 13.6 | Counter |\n| JavaScript | 55 | 47.8 | Counter |\n| Python3 | 77 | 17.3 | RegEx |\n| C# | 71 | 42.2 | Counter |\n| PHP | 68 | 19.6 | Counter |\n| Python3 | 201 | 17.4 | Counter |\n\n\n\n\nBoth the provided approaches offer a clear and efficient solution to the problem. The RegEx approach capitalizes on Python\'s powerful string manipulation capabilities, while the iterative count offers a more intuitive step-by-step breakdown. Understanding both methods provides a holistic view of the problem and its possible solutions. \uD83D\uDCA1\uD83C\uDF20\uD83D\uDC69\u200D\uD83D\uDCBB\uD83D\uDC68\u200D\uD83D\uDCBB | 44 | 1 | ['Greedy', 'PHP', 'Game Theory', 'C++', 'Java', 'Go', 'Python3', 'Rust', 'JavaScript', 'C#'] | 6 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | Count > 2 | count-2-by-votrubac-6f7v | C++\ncpp\nbool winnerOfGame(string colors) {\n int a = 0, b = 0, cnt_a = 0, cnt_b = 0;\n for (auto ch : colors) {\n if (ch == \'A\') {\n | votrubac | NORMAL | 2021-10-18T09:17:06.251200+00:00 | 2021-10-18T09:17:06.251230+00:00 | 2,834 | false | **C++**\n```cpp\nbool winnerOfGame(string colors) {\n int a = 0, b = 0, cnt_a = 0, cnt_b = 0;\n for (auto ch : colors) {\n if (ch == \'A\') {\n if (++cnt_a > 2)\n ++a;\n cnt_b = 0;\n } else {\n if (++cnt_b > 2)\n ++b;\n cnt_a = 0;\n }\n }\n return a > b;\n}\n``` | 36 | 4 | [] | 5 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | 【Video】How we think about a solution - Python, JavaScript, Java, C++ | video-how-we-think-about-a-solution-pyth-7jlk | Welcome to my post! This post starts with "How we think about a solution". In other words, that is my thought process to solve the question. This post explains | niits | NORMAL | 2023-10-02T15:26:34.257707+00:00 | 2023-10-03T06:12:10.486289+00:00 | 508 | false | Welcome to my post! This post starts with "How we think about a solution". In other words, that is my thought process to solve the question. This post explains how I get to my solution instead of just posting solution codes or out of blue algorithms. I hope it is helpful for someone.\n\n# Intuition\nsliding by 3 characters\n\n---\n\n# Solution Video\n\nhttps://youtu.be/XxWF-hv9Ltk\n\n### \u2B50\uFE0F\u2B50\uFE0F Don\'t forget to subscribe to my channel! \u2B50\uFE0F\u2B50\uFE0F\n\n**\u25A0 Subscribe URL**\nhttp://www.youtube.com/channel/UC9RMNwYTL3SXCP6ShLWVFww?sub_confirmation=1\n\nSubscribers: 2,577\nMy initial goal is 10,000\nThank you for your support!\n\n---\n\n# Approach\n### How we think about a solution\n\nAlice and Bob can remove their own characters if they meet the conditions below\n\n---\n\n\n- Alice is only allowed to remove a piece colored \'A\' if both its neighbors are also colored \'A\'. She is not allowed to remove pieces that are colored \'B\'.\n- Bob is only allowed to remove a piece colored \'B\' if both its neighbors are also colored \'B\'. He is not allowed to remove pieces that are colored \'A\'.\n- Alice and Bob cannot remove pieces from the edge of the line.\n\n---\n\nIn this problem, there are countless ways to choose A or B. In such cases, I always start by considering the simplest input to derive patterns or algorithms.\n\nLet\'s think about this case.\n\n```\nInput: "AAA"\n"Alice definitely will win the game."\n```\n\nHow about this.\n```\nInput: "AAABBB"\n"Alice will lose the game."\n```\n\nHow about this.\n```\nInput: "AAABBBAAA"\n"Alice will win the game."\n```\n\n---\n\u2B50\uFE0F Points\n\n- From the simple examples above, Seems like the person with the most occurrences of three identical characters in a row wins.\n\n---\n\nHow about this.\n```\nInput: "AAAABBB"\n\nAAAABBB\n -\nAAABBB\n -\nAAABB\n -\nAABB\n\n"Alice will win the game."\n```\n\n\nHow about this\n\n```\nInput: "AAABBBA"\n\nAAABBBA\n -\nAABBBA\n -\nAABBA\n\n"Alice will lose the game."\n```\n\n---\n\u2B50\uFE0F Points\n\n- When there are four or more identical characters in a row, Alice can increase the count by sliding one by one.\n\nFor example,\n\nAAA = 1\nAAAA = 2\nAAAAA = 3\nAAAAAA = 4\n\n- It doesn\'t seem to matter where to erase from. Because if you have "B" like this\n```\nBBBAAAAAABBB\nBBBAAABAAAABBB\n```\n**the order of A and B is never changed**, so it doesn\'t affect the results.\n\n---\n\nFrom this idea, we should count number of 3 adjacent identical characters, then if Alice\'s count is greater than Bob\'s count, Alice will win the game.\n\nLet\'s see one by one.\n```\nInput: "ABBBBBBBAAA"\n```\n```\nABBBBBBBAAA = Alice: 0, Bob: 0\n---\n```\n```\nABBBBBBBAAA = Alice: 0, Bob: 1\n ---\n```\n```\nABBBBBBBAAA = Alice: 0, Bob: 2\n ---\n```\n```\nABBBBBBBAAA = Alice: 0, Bob: 3\n ---\n```\n```\nABBBBBBBAAA = Alice: 0, Bob: 4\n ---\n```\n```\nABBBBBBBAAA = Alice: 0, Bob: 5\n ---\n```\n```\nABBBBBBBAAA = Alice: 0, Bob: 5\n ---\n```\n```\nABBBBBBBAAA = Alice: 0, Bob: 5\n ---\n```\n```\nABBBBBBBAAA = Alice: 1, Bob: 5\n ---\n```\n```\nOutput: false\n```\n\nOne more edge case,\n\n\n---\n\u2B50\uFE0F Points\n\nAlice goes first, so if Alice\'s count is equal to Bob\'s count, Alice will lose the game.\n\n---\n\n\nBut we can deal with the edge case with `Alice > Bob` instead of `Alice >= Bob` because they are equal, Bob will win the game and we should return `false`. we can return `true` if we only meet `Alice > Bob`.\n\nLet\'s see real algorithm\n\n### Algorithm Overview:\n1. Check the length of the `colors` string to determine if it\'s less than or equal to 2.\n2. Initialize counters for Alice and Bob\'s streaks.\n3. Iterate through the \'colors\' string, excluding the first and last characters, checking for streaks.\n\n### Detailed Explanation:\n1. Check if the length of the `colors` string is less than or equal to 2:\n - If the length is 2 or less, return False since it\'s not possible to determine a winner.\n\n2. Initialize counters for Alice and Bob\'s streaks:\n - Set `alice_count` and `bob_count` to `0` to keep track of consecutive colors for Alice and Bob, respectively.\n\n3. Iterate through the `colors` string to find streaks:\n - Start iterating from the second character to the second-to-last character of the `colors` string.\n - Check if the current color and its adjacent neighbors are the same (a streak of three consecutive colors):\n - If a streak of `AAA` is found, increment `alice_count`.\n - If a streak of `BBB` is found, increment `bob_count`.\n\n4. Determine the winner:\n - Compare `alice_count` and `bob_count`:\n - If Alice has more streaks than Bob, return True, indicating Alice as the winner.\n - Otherwise, return False, indicating Bob as the winner or a tie if counts are equal.\n\n\n\n# Complexity\n\n- Time Complexity: $$O(n)$$\n - The time complexity of this code is O(n), where n is the length of the input \'colors\' string.\n - The for loop iterates through the \'colors\' string, excluding the first and last characters, so the loop runs n-2 times (where n is the length of the `colors` string).\n - Each iteration of the loop performs constant time operations (comparisons and increments), so the overall time complexity is linear in terms of the length of the \'colors\' string.\n\n\n- Space Complexity: $$O(1)$$\n - The space complexity of this code is `O(1)`, which means it uses a constant amount of additional memory regardless of the input size.\n \n - The only variables that are used are `alice_count` and `bob_count`, which are integers and do not depend on the size of the input. Therefore, the space complexity is constant.\n\n```Python []\nclass Solution:\n def winnerOfGame(self, colors: str) -> bool:\n if len(colors) <= 2:\n return False\n\n alice_turn = 0\n bob_turn = 0\n \n for i in range(1, len(colors) - 1):\n if colors[i - 1] == colors[i] == colors[i + 1]:\n if colors[i] == "A":\n alice_turn += 1\n else:\n bob_turn += 1\n \n return alice_turn > bob_turn\n```\n```javascript []\n/**\n * @param {string} colors\n * @return {boolean}\n */\nvar winnerOfGame = function(colors) {\n if (colors.length <= 2) {\n return false;\n }\n\n let aliceTurn = 0;\n let bobTurn = 0;\n\n for (let i = 1; i < colors.length - 1; i++) {\n if (colors[i - 1] === colors[i] && colors[i] === colors[i + 1]) {\n if (colors[i] === \'A\') {\n aliceTurn++;\n } else {\n bobTurn++;\n }\n }\n }\n\n return aliceTurn > bobTurn; \n};\n```\n```java []\nclass Solution {\n public boolean winnerOfGame(String colors) {\n if (colors.length() <= 2) {\n return false;\n }\n\n int aliceTurn = 0;\n int bobTurn = 0;\n\n for (int i = 1; i < colors.length() - 1; i++) {\n if (colors.charAt(i - 1) == colors.charAt(i) && colors.charAt(i) == colors.charAt(i + 1)) {\n if (colors.charAt(i) == \'A\') {\n aliceTurn++;\n } else {\n bobTurn++;\n }\n }\n }\n\n return aliceTurn > bobTurn; \n }\n}\n```\n```C++ []\nclass Solution {\npublic:\n bool winnerOfGame(string colors) {\n if (colors.length() <= 2) {\n return false;\n }\n\n int aliceTurn = 0;\n int bobTurn = 0;\n\n for (int i = 1; i < colors.length() - 1; i++) {\n if (colors[i - 1] == colors[i] && colors[i] == colors[i + 1]) {\n if (colors[i] == \'A\') {\n aliceTurn++;\n } else {\n bobTurn++;\n }\n }\n }\n\n return aliceTurn > bobTurn; \n }\n};\n```\n\n\n---\n\n\nThank you for reading my post.\n\u2B50\uFE0F Please upvote it and don\'t forget to subscribe to my channel!\n\nMy next post for daily coding challenge on October 3rd, 2023\nhttps://leetcode.com/problems/number-of-good-pairs/solutions/4122338/video-how-we-think-about-a-solution-python-javascript-java-c/ | 32 | 0 | ['C++', 'Java', 'Python3', 'JavaScript'] | 2 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | 💯Faster✅💯Lesser✅Efficient🔥Simple🔥Time Complexity O(n)🚀 | fasterlesserefficientsimpletime-complexi-7m2l | \uD83D\uDE80 Hi, I\'m Mohammed Raziullah Ansari, and I\'m excited to share multiple ways to solve this question with detailed explanation of each approach:\n\n | Mohammed_Raziullah_Ansari | NORMAL | 2023-10-02T01:24:29.052314+00:00 | 2023-10-02T01:50:58.761393+00:00 | 2,100 | false | # \uD83D\uDE80 Hi, I\'m [Mohammed Raziullah Ansari](https://leetcode.com/Mohammed_Raziullah_Ansari/), and I\'m excited to share multiple ways to solve this question with detailed explanation of each approach:\n\n# Problem Explaination: \nThe problem here involves a sequence of colored pieces arranged in a linear order, such as an array or a linked list. Each piece has a specific color. The objective is to remove pieces from this sequence when both of their neighboring pieces have the same color.\n\n\n\n# Problem Approach: \n\n- Initialize two integer variables, count_A and count_B, to zero.\n- Loop through the input string colors from index 1 to len(colors) - 2. This loop iterates through the string, excluding the first and last characters because it checks for consecutive triples.\n- Inside the loop, check if the character at the current index i and the characters at i - 1 and i + 1 are all the same. If they are, it indicates a consecutive triple of the same character.\n- If a triple is found, check the character at index i. If it\'s \'A\', increment count_A by 1. If it\'s \'B\', increment count_B by 1.\n- After the loop, compare count_A and count_B to determine the winner.\n- Return true if count_A is greater than count_B, indicating that player A wins; otherwise, return false.\n# Complexity\n- \u23F1\uFE0F Time Complexity: O(n), where \'n\' is the length of the input string colors.\n\n- \uD83D\uDE80 Space Complexity: O(1) - constant space is used.\n\n# Code\n```Python []\nclass Solution:\n def winnerOfGame(self, colors: str) -> bool:\n count_A = 0\n count_B = 0\n for i in range(1, len(colors) - 1):\n if colors[i - 1] == colors[i] == colors[i + 1]:\n if colors[i] == \'A\':\n count_A += 1\n else:\n count_B += 1\n return count_A > count_B\n\n```\n```Java []\npublic class Solution {\n public boolean winnerOfGame(String colors) {\n int countA = 0;\n int countB = 0;\n for (int i = 1; i < colors.length() - 1; i++) {\n if (colors.charAt(i - 1) == colors.charAt(i) && colors.charAt(i) == colors.charAt(i + 1)) {\n if (colors.charAt(i) == \'A\') {\n countA++;\n } else {\n countB++;\n }\n }\n }\n return countA > countB;\n }\n}\n\n```\n```C++ []\nclass Solution {\npublic:\n bool winnerOfGame(string colors) {\n int countA = 0, countB = 0;\n int n = colors.length();\n for (int i = 0; i < n - 2; i++) {\n if (colors[i] == \'A\' && colors[i + 1] == \'A\' && colors[i + 2] == \'A\') {\n countA++;\n }\n if (colors[i] == \'B\' && colors[i + 1] == \'B\' && colors[i + 2] == \'B\') {\n countB++;\n }\n }\n return countA > countB;\n }\n};\n\n```\n```C []\nbool winnerOfGame(char *colors) {\n int count_A = 0;\n int count_B = 0;\n for (int i = 1; i < strlen(colors) - 1; i++) {\n if (colors[i - 1] == colors[i] && colors[i] == colors[i + 1]) {\n if (colors[i] == \'A\') {\n count_A++;\n } else {\n count_B++;\n }\n }\n }\n return count_A > count_B;\n}\n\n```\n\n# \uD83C\uDFC6Note: \nI will soon add more three methods to solve this code in Python, Java, C++, C.\n\n# \uD83D\uDCA1 I invite you to check out my profile for detailed explanations and code for each method. Happy coding and learning! \uD83D\uDCDA | 30 | 4 | ['Array', 'Linked List', 'Math', 'Two Pointers', 'String', 'Stack', 'C', 'C++', 'Java', 'Python3'] | 11 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | ☑99.54% || Cpp || Java || python || javascript || Greedy 💯 | 9954-cpp-java-python-javascript-greedy-b-uzgl | Read article Explaination and codes : https://www.nileshblog.tech/leetcode-2038-remove-colored-pieces/\n\nJava \nC++\nPython \nJavascript\n\n# the Problem\n Lee | user6845R | NORMAL | 2023-10-02T00:36:03.527540+00:00 | 2023-10-02T01:00:51.726926+00:00 | 1,537 | false | # Read article Explaination and codes : https://www.nileshblog.tech/leetcode-2038-remove-colored-pieces/\n\nJava \nC++\nPython \nJavascript\n\n# the Problem\n Leetcode 2038 presents us with an array of colored pieces, each represented by a lowercase letter. Our goal is to remove all pieces with the same color if they have neighbors with the same color. In simpler terms, imagine a row of beads, and we want to remove adjacent beads of the same color. We\'ll keep doing this until no more removals are possible.\n \n \n\n \n To solve this problem, we\'ll use a greedy strategy. Greedy algorithms aim to make the best decision at each step, leading to an optimal solution. In our case, we\'ll repeatedly scan the array to find and remove adjacent pieces with the same color until we can\'t make any more removals.\n **time complexity is O(N)**\n \n step-by-step\n1. Initialize an vector / array cnts to keep track of the points collected by both players. Initially, both players have zero points.\n1. Initialize currrent to the first color in the string, and count to zero to keep track of the consecutive balls of the current color.\n1. Iterate through the string of colors:\n1. If the current ball has the same color as the previous one, increment count.\n1. If count reaches or exceeds 3, update the score in the cnts array for the current player (\'A\' or \'B\').\n1. If the current ball has a different color, reset cur to the new color and reset count to 1.\n1. After processing the entire string, the function returns true if player \'A\' (represented by cnts[0]) has more points than player \'B\' (represented by cnts[1]), indicating that player \'A\' is the winner.\n\n\n | 21 | 2 | ['C', 'Python', 'Java', 'JavaScript'] | 2 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | Very Easy Java Solution With Detailed Explation| Beats 70% | very-easy-java-solution-with-detailed-ex-yhia | Idea behind it is that you need to count the number of triplets of both A and B \nint a -> number of triplets of \'A\'\nint b -> number of triplets of \'B\'\nif | anishdhingra | NORMAL | 2022-07-16T16:31:57.740908+00:00 | 2022-07-18T05:14:18.632692+00:00 | 2,152 | false | Idea behind it is that you need to count the number of triplets of both A and B \nint a -> number of triplets of \'A\'\nint b -> number of triplets of \'B\'\nif(b>=a) BOB wins else Alice wins\n\nAs Alice has to make a move first so if she wants to win there should be atleast 1 more triplets of A than B\n\nEg There are 4 triplets of both A and B (a=4, b=4) \n1. Alice removes 1 A (a=3, b=4)\n2. Bob removes 1 B (a=3, b=3)\n3. (a=2, b=3)\n4. (a=2, b=2)\n5. (a=1, b=2)\n6. (a=1, b=1)\n7. (a=0, b=1)\n\nClearly Alice will lose if both have same number of triplets\nCode \n\n```\nclass Solution {\n public boolean winnerOfGame(String s) {\n //count the triplets\nint n = s.length();\n \n int a=0;\n int b=0;\n \n for(int i=1; i<n-1; i++)\n {\n if(s.charAt(i)==\'A\' && s.charAt(i-1)==\'A\' && s.charAt(i+1)==\'A\' )\n a++;\n else if(s.charAt(i)==\'B\' && s.charAt(i-1)==\'B\' && s.charAt(i+1)==\'B\' )\n b++;\n \n }\n if(a<=b)\n return false;\n else\n return true;\n \n \n }\n}\n```\nIf this helped you then please UpVote\nThanks\n\n\n | 21 | 0 | ['Java'] | 3 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | [Java/Python 3] Count moves and compare. | javapython-3-count-moves-and-compare-by-l6t96 | Method 1:\n\n1. For each consecutive A or Bs, if its size is at least 3, then starting from the size of 3 count into moves of Alice or Bob, respectively.\n2. Co | rock | NORMAL | 2021-10-16T16:03:32.450772+00:00 | 2021-10-16T16:32:32.820902+00:00 | 2,319 | false | **Method 1:**\n\n1. For each consecutive `A` or `B`s, if its size is at least `3`, then starting from the size of `3` count into moves of `Alice` or `Bob`, respectively.\n2. Compare to determine the winner.\n```java\n public boolean winnerOfGame(String colors) {\n char prev = \'C\';\n int a = 0, b = 0;\n for (int i = 0, cnt = 0; i < colors.length(); ++i) {\n char cur = colors.charAt(i);\n if (cur == prev) {\n if (++cnt > 2) {\n if (cur == \'A\') {\n ++a;\n }else {\n ++b;\n }\n }\n }else {\n cnt = 1;\n }\n prev = cur;\n }\n return a > b;\n }\n```\n```python\n def winnerOfGame(self, colors: str) -> bool:\n a = b = cnt = 0\n prev = \'C\' # dummy value.\n for cur in colors:\n if cur == prev:\n cnt += 1\n if cnt > 2:\n if cur == \'A\':\n a += 1\n else:\n b += 1\n else:\n cnt = 1\n prev = cur\n return a > b \n```\n\n----\n\n**Method 2: Count "AAA" or "BBB"** - credit to **@lokeshsenthilkumar**\n\n```java\n public boolean winnerOfGame(String colors) {\n int a = 0, b = 0;\n for (int i = 2; i < colors.length(); ++i) {\n if (colors.charAt(i - 2) == colors.charAt(i - 1) && colors.charAt(i - 1) == colors.charAt(i)) {\n if (colors.charAt(i) == \'A\') {\n ++a;\n }else {\n ++b;\n }\n }\n }\n return a > b;\n }\n```\n\n```python\n def winnerOfGame(self, colors: str) -> bool:\n a = b = 0\n for i in range(2, len(colors)):\n if colors[i - 2] == colors[i - 1] == colors[i]:\n if colors[i] == \'A\':\n a += 1\n else:\n b += 1\n return a > b\n```\n**Analysis:**\n\nTime: `O(n)`, space: `O(1)`, where `n = colors.length()`. | 15 | 1 | ['Java', 'Python3'] | 3 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | Easy 1 line solution (Java RegExp) | easy-1-line-solution-java-regexp-by-serg-yx3o | Java []\nclass Solution {\n public boolean winnerOfGame(String colors) {\n return colors.replaceAll("A{3,}", "AA").length() < colors.replaceAll("B{3,} | SergeyZhirkov | NORMAL | 2023-10-02T01:05:20.945766+00:00 | 2024-09-22T19:11:33.265677+00:00 | 1,015 | false | ```Java []\nclass Solution {\n public boolean winnerOfGame(String colors) {\n return colors.replaceAll("A{3,}", "AA").length() < colors.replaceAll("B{3,}", "BB").length();\n }\n}\n```\n\n\n | 14 | 2 | ['Java'] | 5 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | C++ Counting | c-counting-by-lzl124631x-8z3s | See my latest update in repo LeetCode\n\n## Solution 1. Counting\n\nEach continuous segment of A or B of length cnt has cnt - 2 pieces avaiable for the players | lzl124631x | NORMAL | 2021-10-16T16:19:30.483420+00:00 | 2021-10-16T16:19:30.483447+00:00 | 1,585 | false | See my latest update in repo [LeetCode](https://github.com/lzl124631x/LeetCode)\n\n## Solution 1. Counting\n\nEach continuous segment of `A` or `B` of length `cnt` has `cnt - 2` pieces avaiable for the players to pick. We sum these `cnt - 2`s up and Alice wins if her sum is greater.\n\n```cpp\n// OJ: https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/\n// Author: github.com/lzl124631x\n// Time: O(N)\n// Space: O(1)\nclass Solution {\npublic:\n bool winnerOfGame(string s) {\n int sum[2] = {};\n for (int i = 0, N = s.size(); i < N;) {\n int c = s[i], cnt = 0;\n while (i < N && c == s[i]) ++i, ++cnt;\n sum[c - \'A\'] += max(0, cnt - 2);\n }\n return sum[0] > sum[1];\n }\n};\n``` | 13 | 1 | [] | 1 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | JAVA || O(N) Time O(1) Space || Faster || Easiest Approach || Easy To Understand | java-on-time-o1-space-faster-easiest-app-t3no | \n\n# YouTube Video:\nhttps://youtu.be/8vnMbZ_LBR0\nPlease subscribe to my YouTube Channel.\n# Intuition\n- To determine the winner, we need to count how many s | millenium103 | NORMAL | 2023-10-02T04:20:13.287878+00:00 | 2023-10-02T06:27:37.263308+00:00 | 840 | false | \n\n# YouTube Video:\n[https://youtu.be/8vnMbZ_LBR0]()\nPlease subscribe to my YouTube Channel.\n# Intuition\n- To determine the winner, we need to count how many sequences of consecutive \'A\'s and \'B\'s are present in the given string, where each sequence has a length of 3 or more. The reason is that if a player has three consecutive pieces they can remove, they will continue doing so on their turn until they can\'t. The player with more such sequences has the advantage.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialize variables `aCnt` and `bCnt` to `0` to keep track of the count of sequences for Alice and Bob, respectively.\n2. Initialize temporary variables `aTemp` and `bTemp` to `0` to keep track of consecutive \'A\' and \'B\' pieces encountered during the traversal.\n3. Iterate through the characters of the input string `colors`.\n - If the current character is `\'A\'`, reset `bTemp` to `0` and increment `aTemp`. If `aTemp` becomes 3 or more, increment `aCnt`.\n - If the current character is `\'B\'`, reset `aTemp` to `0` and increment `bTemp`. If `bTemp` becomes 3 or more, increment `bCnt`.\n4. After processing the entire string, compare `aCnt` and `bCnt` to determine the winner. If `aCnt` is greater than `bCnt`, Alice wins; otherwise, Bob wins.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: `O(N)`, where `N` is the length of the input string colors. We iterate through the string once.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: `O(1)`, as we use a constant amount of extra space for variables.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public boolean winnerOfGame(String colors) {\n int aCnt=0, bCnt=0;\n int aTemp=0, bTemp=0;\n\n for(int i=0;i<colors.length();i++)\n {\n char c = colors.charAt(i);\n if(c==\'A\')\n {\n bTemp=0;\n aTemp+=1;\n if(aTemp>=3)\n {\n aCnt+=1;\n }\n }\n else\n {\n aTemp=0;\n bTemp+=1;\n if(bTemp>=3)\n {\n bCnt+=1;\n }\n }\n }\n return aCnt>bCnt;\n }\n}\n```\n\n | 12 | 0 | ['String', 'Java'] | 4 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | PYTHON || ✔️ EXPLANATION FOR ALL ✔️|| | python-explanation-for-all-by-karan_8082-fdt0 | UPVOTE IF HELPFUL\n\nWe count the continous occurances of characters.\nIf occurance is more than 2. -> Number of turns possible is ( Occurances - 2 )\nCompute t | karan_8082 | NORMAL | 2022-06-05T04:04:20.562634+00:00 | 2022-06-05T04:05:14.020496+00:00 | 1,952 | false | **UPVOTE IF HELPFUL**\n\nWe count the continous occurances of characters.\nIf occurance is more than 2. -> Number of turns possible is **( Occurances - 2 )**\nCompute this and give answer on basis which player get more turns.\n\n\n\n\n\n```\nclass Solution:\n def winnerOfGame(self, s: str) -> bool:\n a=[]\n p="C"\n for i in s:\n if i==p:\n a[-1]+=1\n else:\n p=i\n a.append(1)\n odd,even=0,0\n for i in range(len(a)):\n if i%2:\n odd += max(0,a[i]-2)\n else:\n even += max (0,a[i]-2)\n if s[0]=="A" and even>odd:\n return True\n if s[0]=="B" and odd>even:\n return True\n return False\n```\n\n | 10 | 1 | ['Python', 'Python3'] | 1 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | C++ Two Simple and Short Solutions, Explained | c-two-simple-and-short-solutions-explain-bw50 | Solution I:\nWe count all the pieces that Alice and Bob can remove.\nThey can only remove the middle of three As or Bs in a row.\nSo all we need to do is count | yehudisk | NORMAL | 2021-10-17T12:54:29.155597+00:00 | 2021-10-17T12:54:29.155632+00:00 | 726 | false | **Solution I:**\nWe count all the pieces that Alice and Bob can remove.\nThey can only remove the middle of three `A`s or `B`s in a row.\nSo all we need to do is count the `AAA`s and `BBB`s.\n```\nclass Solution {\npublic:\n bool winnerOfGame(string colors) {\n if (colors.size() < 3) return false;\n int a = 0, b = 0;\n for (int i = 0; i < colors.size()-2; i++) {\n if (colors.substr(i, 3) == "AAA") a++;\n else if (colors.substr(i, 3) == "BBB") b++;\n }\n return a > b;\n }\n};\n```\n****\n**Solution II:**\nInstead of counting the sets of `AAA`s and `BBB`s, we can decrease the runtime by just checking if the previous and next letters are the same.\n```\nclass Solution {\npublic:\n bool winnerOfGame(string colors) {\n if (colors.size() < 3) return false;\n int a = 0, b = 0;\n for (int i = 1; i < colors.size()-1; i++) {\n if (colors[i-1] == colors[i] && colors[i] == colors[i+1]) {\n if (colors[i] == \'A\') a++;\n else b++;\n }\n }\n return a > b;\n }\n};\n```\n**Like it? please upvote!** | 10 | 0 | ['C'] | 0 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | using KMP in c++ | using-kmp-in-c-by-raushan043-unif | \nclass Solution {\n \n\n int findcount(vector<int>&points,string search,string pattern)\n{\n int i=0;\n int j=0;\n int count=0;\n while(i | raushan043 | NORMAL | 2021-10-16T16:05:27.400897+00:00 | 2021-10-16T16:05:27.400931+00:00 | 557 | false | ```\nclass Solution {\n \n\n int findcount(vector<int>&points,string search,string pattern)\n{\n int i=0;\n int j=0;\n int count=0;\n while(i<search.size())\n {\n if(search[i]==pattern[j])\n {\n i++;\n j++;\n }\n else\n {\n if(j!=0)\n {\n j=points[j-1];\n }\n else\n {\n i++;\n }\n }\n if(j==points.size())\n {\n // cout<<i-points.size()<<endl;\n // break;\n count++;\n j=points[j-1];\n }\n\n }\n return count;\n}\npublic:\n bool winnerOfGame(string colors) {\n \n vector<int>temp1={0,1,2};\n string pattern="AAA";\n string pattern2="BBB";\n int countalice= findcount(temp1,colors,pattern);\n int countbob= findcount(temp1,colors,pattern2);\n \n if(countalice>countbob)\n {\n return true;\n }\n else\n {\n return false;\n }\n \n }\n};\n``` | 9 | 4 | [] | 0 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | VERY EASY AND FAST APPROACH-(O(N)) | very-easy-and-fast-approach-on-by-carped-7fcu | Intuition\n Describe your first thoughts on how to solve this problem. \nThe number of moves a player can make doesn\'t depend on what the other player does.\n\ | Carpediem02 | NORMAL | 2023-10-02T04:10:37.472356+00:00 | 2023-10-02T04:10:37.472385+00:00 | 26 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe number of moves a player can make doesn\'t depend on what the other player does.\n\nIf a group of n consecutive pieces has the same color, the player can take n - 2 of those pieces if n is greater than or equal to 3\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nJust Implement the Intution\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n```\nO(N)\n```\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n```\nO(1)\n```\n\n# Code\n```\nclass Solution {\npublic:\n bool winnerOfGame(string colors) {\n int a=0;\n int b=0;\n bool alice=colors[0]==\'A\'?1:0;\n int count=1;\n for(int i=1;i<colors.size();i++){\n if(alice){\n if(colors[i]==\'A\'){\n count++;\n }\n else{\n alice=0;\n if(count>=3){\n a=a+count-2;\n }\n count=1;\n }\n }\n else{\n if(colors[i]==\'B\'){\n count++;\n }\n else{\n alice=1;\n if(count>=3)b=b+count-2;\n count=1;\n }\n }\n }\n if(count>=3){\n if(colors[colors.size()-1]==\'A\')a+=count-2;\n else b+=count-2;\n }\n if(a>b)return 1;\n return 0;\n }\n};\n``` | 8 | 0 | ['C++'] | 0 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | Easy To Understand || C++ || Python || Java | easy-to-understand-c-python-java-by-me_a-ppv3 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nThe code iterates through the colors string and checks for consecutive oc | me_avi | NORMAL | 2023-10-02T03:12:52.358006+00:00 | 2023-10-02T03:12:52.358024+00:00 | 1,003 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe code iterates through the colors string and checks for consecutive occurrences of \'A\' and \'B\'.\nIt maintains two counters, \'count\' for consecutive \'A\'s and \'count1\' for consecutive \'B\'s.\nWhenever three \'A\'s or three \'B\'s occur consecutively, it increments the respective counter.\nFinally, it returns \'true\' if \'count\' is greater than \'count1\', indicating that \'A\' has more consecutive occurrences than \'B\', otherwise it returns \'false\'.\n# Complexity\n- Time complexity:\nTime complexity: O(N), where N is the length of the input string colors.\n\n- Space complexity:\nSpace complexity: O(1), as it uses a constant amount of extra space to store the counters count and count1.\n\n# Code\n```C++ []\nclass Solution {\npublic:\n bool winnerOfGame(string colors) {\n int count=0,count1=0;\n for(int i=1; i<colors.length()-1;i++)\n {\n if(colors[i] ==\'A\' && colors[i-1]==\'A\' && colors[i+1]==\'A\')\n {\n count++;\n }\n else if(colors[i]==\'B\' && colors[i-1]==\'B\' && colors[i+1]==\'B\')\n {\n count1++;\n }\n }\n return count>count1;\n }\n};\n```\n```python []\nclass Solution:\n def winnerOfGame(self, colors: str) -> bool:\n count = 0\n count1 = 0\n for i in range(1, len(colors) - 1):\n if colors[i] == \'A\' and colors[i - 1] == \'A\' and colors[i + 1] == \'A\':\n count += 1\n elif colors[i] == \'B\' and colors[i - 1] == \'B\' and colors[i + 1] == \'B\':\n count1 += 1\n return count > count1\n\n```\n```Java []\npublic class Solution {\n public boolean winnerOfGame(String colors) {\n int count = 0;\n int count1 = 0;\n for (int i = 1; i < colors.length() - 1; i++) {\n if (colors.charAt(i) == \'A\' && colors.charAt(i - 1) == \'A\' && colors.charAt(i + 1) == \'A\') {\n count++;\n } else if (colors.charAt(i) == \'B\' && colors.charAt(i - 1) == \'B\' && colors.charAt(i + 1) == \'B\') {\n count1++;\n }\n }\n return count > count1;\n }\n}\n\n```\n\n\n\n\n\n | 8 | 0 | ['C++'] | 5 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | Java solution. O(n) | java-solution-on-by-ikromjonov_marufjon-p9in | \nclass Solution {\n public boolean winnerOfGame(String colors) {\n int countA = 0;\n int countB = 0;\n\n char[] mass = colors.toCharArr | ikromjonov_marufjon | NORMAL | 2021-10-16T17:06:25.468127+00:00 | 2021-10-16T17:06:25.468211+00:00 | 412 | false | ```\nclass Solution {\n public boolean winnerOfGame(String colors) {\n int countA = 0;\n int countB = 0;\n\n char[] mass = colors.toCharArray();\n\n for (int i = 1; i < mass.length - 1; i++) {\n if (mass[i - 1] == \'A\' && mass[i] == \'A\' && mass[i + 1] == \'A\') {\n countA ++;\n }\n if (mass[i - 1] == \'B\' && mass[i] == \'B\' && mass[i + 1] == \'B\') {\n countB ++;\n }\n }\n return countA > countB;\n }\n}\n``` | 8 | 0 | [] | 2 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | Easy C++ || Count approach O(N) | easy-c-count-approach-on-by-code1511-lmde | \nclass Solution {\npublic:\n bool winnerOfGame(string colors) {\n int a=0;\n int b=0;\n for(int i=1;i<colors.size();i++){\n | code1511 | NORMAL | 2021-10-16T16:03:28.351883+00:00 | 2021-10-16T16:04:14.101844+00:00 | 724 | false | ```\nclass Solution {\npublic:\n bool winnerOfGame(string colors) {\n int a=0;\n int b=0;\n for(int i=1;i<colors.size();i++){\n if(colors[i-1]==colors[i] && colors[i+1]==colors[i]){\n if(colors[i]==\'A\')\n a++;\n else\n b++;\n }\n }\n \n return a>b;\n }\n};\n``` | 8 | 2 | ['C', 'C++'] | 1 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | 🔥Beats 99.80% 📈 O(n) Easy Beginner Approach | beats-9980-on-easy-beginner-approach-by-kwlqq | \n# Intuition\nWhoever has the most number of consecutive 3 colors wins.\nSo we need to count the number of moves each person can make.\n\nFor the return value: | atsreecha | NORMAL | 2023-10-04T14:36:41.877535+00:00 | 2023-11-15T03:29:45.274877+00:00 | 66 | false | \n# Intuition\nWhoever has the most number of consecutive 3 colors wins.\nSo we need to count the number of moves each person can make.\n\nFor the return value: if Alice has more moves than Bob, we `return True`\nelse, we `return False`.\n\n> One might question what happens if number of possible moves of Alice and Bob are _equal_!\n\n> In that case, during the game, Alice and Bob both would have completed their moves and since both have the same number of moves and Since Alice plays first, Bob would have finished the last move. \nThus Alice will have to play and she will be left with no more moves to play.\nThus we `return False` even in that case. \n\n### Thing to note tho :\nA substring `"AAAAAA"` gives Alice a total of 4 Moves and not just 2.\n\n**Move 1** `"AA _A_ AAA"`\n**Move 2** `"AA _A_ AA"`\n**Move 3** `"AA _A_ A"`\n**Move 4** `"AA _A_ "`\n\nOn contrary to the usual misunderstanding of `A "A" AA "A" A`\n\n# Approach\n> The first approach will be to count the number of occurences of `"AAA"` and `"BBB"` substring in **string colors**. \nBut the `str.count(substring)` will not come handy in this case as it does not count `AAAAAAA` as 4 occurences, but as just 2!! (this is due to the poiunter going beyond the 2nd A i the substring while counting the first occurence of AAA)\n\n**Thing to learn :** \nString functions are just not always handy. They do pull you done soometimes due to internal technicalities. Never rely on them for CP\n\n##### Soo.....\n#### Lets solve it the right way ! \n\nTo find the total number of coonsicutive 3 like colors, we can iterate through the array and find if the next element and its next element are the same. We store the total possible moves in a variable for each color(player).\n\n# Complexity\n- Time complexity: O(n)\n- Space complexity : O(1)\n\n# Code\n```py\nclass Solution:\n def winnerOfGame(self, colors: str) -> bool:\n a,b=0,0\n for i,ii,iii in zip(colors,colors[1:],colors[2:]):\n if \'A\'==i==ii==iii:a+=1\n elif \'B\'==i==ii==iii:b+=1\n return b<a\n\n```\n\n\n> \n\n> _"catphrase"_\n | 7 | 0 | ['Python3'] | 0 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | ✅Simple and Clear Python3 Code✅ | simple-and-clear-python3-code-by-moazmar-nvsz | \n\n# Code\n\nclass Solution:\n def winnerOfGame(self, colors: str) -> bool:\n alice,bob = 0,0\n n=len(colors)\n for i in range(n-2):\n | moazmar | NORMAL | 2023-10-02T01:40:51.722821+00:00 | 2023-10-02T01:40:51.722844+00:00 | 16 | false | \n\n# Code\n```\nclass Solution:\n def winnerOfGame(self, colors: str) -> bool:\n alice,bob = 0,0\n n=len(colors)\n for i in range(n-2):\n x=colors[i:i+3]\n if x==\'AAA\':\n alice+=1\n elif x== \'BBB\':\n bob+=1\n\n return alice>bob\n``` | 7 | 0 | ['Python3'] | 0 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | Efficient || Faster || Optimized Solution || Easy to understand code || O(n) time complexity :) | efficient-faster-optimized-solution-easy-7e2k | Intuition\n<-- To count the number of valid moves Alice and Bob can make based on the given rules and determine the winner based on who can make more valid move | Ankita_Chaturvedi | NORMAL | 2023-10-02T01:37:37.484513+00:00 | 2023-10-02T16:52:43.801376+00:00 | 644 | false | # Intuition\n<-- To count the number of valid moves Alice and Bob can make based on the given rules and determine the winner based on who can make more valid moves.\n\n# Approach\n1. Initialize counters for Alice\'s and Bob\'s valid moves.\n2. Iterate through the string, checking for consecutive \'A\'s and \'B\'s.\n3. Increment the respective counter for each valid move.\n4. Determine the winner by comparing the counters.\n\n# Complexity\nTime complexity: O(n) as we iterate through the string once.\nSpace complexity: O(1) as we use a constant amount of extra space.\n\n# Code\n```\nclass Solution {\npublic:\n bool winnerOfGame(string colors) {\n int count1=0,c2=0;\n for(int i=1;i<colors.size()-1;i++){\n if(colors[i]==\'A\' && colors[i-1]==\'A\' && colors[i+1]==\'A\' ){\n count1++;\n }\n else if(colors[i]==\'B\' && colors[i-1]==\'B\' && colors[i+1]==\'B\' ){\n c2++;\n } \n }\n return count1>c2;\n }\n};\n``` | 7 | 0 | ['Counting', 'C++'] | 5 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | Beats 100%||C/C++/Python find AAA... & BBB... | beats-100ccpython-find-aaa-bbb-by-anwend-been | Intuition\n Describe your first thoughts on how to solve this problem. \nThis is a game. Find the substring \'AAA....\' and \'BBB....\'.\n\nLet\'s see some case | anwendeng | NORMAL | 2023-10-02T00:34:16.498343+00:00 | 2023-10-02T06:49:14.781024+00:00 | 918 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis is a game. Find the substring \'AAA....\' and \'BBB....\'.\n\nLet\'s see some cases (number of Alice can take vs number of Bob can take )\n```\nAAABABB ->1 vs 0 result:1\nAA ->0 vs 0 result:0\nABBBBBBBAAA->1 vs 5 result:0\nAAAABBBB ->2 vs 2 result:0\n\n```\n[Please turn English subtitles if necessary]\n[https://youtu.be/heg4EVIqfRE?si=2gS9VCNvYSN-CWnw](https://youtu.be/heg4EVIqfRE?si=2gS9VCNvYSN-CWnw)\n\n2nd C++ approach uses substr beats 100%\n# Approach\n<!-- Describe your approach to solving the problem. -->\nIf there is substring \'AAA...A\' with n \'A\' (n>2) which means Alice can take (n-2) \'A\' from this substring. The solving question becomes just finding the substring \'AAA....\' and \'BBB....\'!\n\n1. `int player[2] = {0};` initializes an integer array player with two elements, both initialized to zero. This array is used to keep track of the scores of two players, where index 0 represents Alice and index 1 represents Bob.\n\n2. `int n = colors.size();` calculates the length of the input string colors.\n\n3. `int len = 1;` initializes an integer variable len to 1. This variable is used to keep track of the current streak of consecutive colors.\n\n4. `char prev = colors[0];` initializes a character variable prev with the first character of the input string colors. This variable is used to keep track of the previously encountered color.\n\n5. The `#pragma unroll` directive is a compiler hint, but in this context, it doesn\'t have any effect on the code\'s behavior.\n\nThe code then enters a for loop that iterates through the characters of the colors string starting from the second character (index 1).\n\n6. Inside the loop:\n`char c = colors[i];` assigns the current character in the loop to the variable c.\n`if (prev == c) len++;` checks if the current character c is the same as the previous character prev. If they are the same, it increments the len variable to track the length of the current streak.\nelse len = 1; if the current character is different from the previous one, it resets the len variable to 1, indicating the start of a new streak.\n`if (len > 2) player[c - \'A\']++;` checks if the streak length len is greater than 2. If so, it increments the score of the player corresponding to the current character c. The subtraction of \'A\' from c converts the character to an integer index (e.g., \'A\' becomes 0, \'B\' becomes 1).\n`prev = c;` updates the prev variable to the current character for the next iteration.\n\n7. Finally, the function returns `player[0] > player[1];`, which compares the scores of Alice and Bob. If Alice\'s score is greater than Bob\'s score, it returns true, indicating that Alice is the winner; otherwise, it returns false, indicating that Bob is the winner.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n $$O(n)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n $$O(1)$$\n# Code : C code runtime 8ms, C++ runtime 16 ms Beats 99.76%\n```C []\nbool winnerOfGame(char * colors){\n int player[2]={0};// 0 for Alice, 1 for Bob\n int n=strlen(colors);\n int len=1;\n char prev=colors[0];\n\n #pragma unroll\n for(register int i=1; i<n; i++ ){\n char c=colors[i];\n if (prev==c) len++;\n else len=1;\n if (len>2) player[c-\'A\']++;\n prev=c;\n } \n return player[0]>player[1];\n\n}\n```\n```C++ []\n\nclass Solution {\npublic:\n bool winnerOfGame(string colors) {\n int player[2]={0};// 0 for Alice, 1 for Bob\n int n=colors.size();\n int len=1;\n char prev=colors[0];\n #pragma unroll\n for(int i=1; i<n; i++){\n char c=colors[i];\n if (prev==c) len++;\n else len=1;\n if (len>2) player[c-\'A\']++;\n prev=c;\n } \n // cout<<player[0]<<" vs "<<player[1]<<endl;\n return player[0]>player[1];\n }\n};\n```\n# Python solution\n```\nclass Solution:\n def winnerOfGame(self, colors: str) -> bool:\n player=[0, 0]\n len=1\n prev=colors[0]\n for c in colors[1:]:\n if prev==c: len+=1\n else: len=1\n if len>2: \n player[ord(c)-ord(\'A\')]+=1\n prev=c\n return player[0]>player[1]\n \n```\n# C++ other approach runtime 12 ms Beats 100%\n```\nclass Solution {\npublic:\n bool winnerOfGame(string colors) {\n int player[2]={0};\n int n=colors.size();\n if (n<3) return 0;//Be careful for edge cases\n int prev2=colors[0], prev1=colors[1];\n\n #pragma unroll\n for(char& c: colors.substr(2)) {\n if (c==prev1 && c==prev2) player[c-\'A\']++;\n prev2=prev1, prev1=c;\n }\n// cout<<player[0]<<" vs "<<player[1]<<endl;\n return player[0]>player[1];\n }\n}\n```\n | 6 | 0 | ['String', 'Game Theory'] | 1 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | ✅Easy explanation ever - O(n) solution | easy-explanation-ever-on-solution-by-sak-7l0w | Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition behind this code is to determine the winner of a game played by Alice and | saket_1 | NORMAL | 2023-10-02T07:12:09.248221+00:00 | 2023-10-02T07:12:09.248244+00:00 | 499 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind this code is to determine the winner of a game played by Alice and Bob, where they take alternating turns removing pieces based on specific rules about adjacent colors in the input string.\n\n---\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n The approach involves iterating through the input string to count the consecutive valid moves each player can make and then comparing these counts to determine the winner.\n\n---\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe code\'s time complexity is O(n), where n is the length of the input string colors, as it iterates through the entire string once.\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity is O(1) because it uses a constant amount of extra space to store the counts of valid moves for Alice and Bob, regardless of the input string\'s size.\n\n---\n# Do upvote if you like the explanation and solution \u2705\n# Code\n```\nclass Solution {\npublic:\n bool winnerOfGame(string colors) {\n int n = colors.length();\n int aliceWins = 0, bobWins = 0;\n for (int i = 1; i < n - 1; ++i) {\n if (colors[i - 1] == \'A\' && colors[i] == \'A\' && colors[i + 1] == \'A\') {\n aliceWins++;\n } else if (colors[i - 1] == \'B\' && colors[i] == \'B\' && colors[i + 1] == \'B\') {\n bobWins++;\n }\n }\n if (aliceWins > bobWins) {\n return true;\n }\n return false;\n }\n};\n``` | 5 | 1 | ['Math', 'String', 'Greedy', 'C', 'Game Theory', 'Python', 'C++', 'Java', 'Python3', 'JavaScript'] | 2 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | 100% Acceptance rate with O(n) Solution - Detailed explanation Ever Check it out | 100-acceptance-rate-with-on-solution-det-kyll | Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem presents a game played between Alice and Bob on a line of colored pieces, r | anshuP_cs24 | NORMAL | 2023-10-02T07:03:02.843874+00:00 | 2023-10-02T07:03:02.843898+00:00 | 580 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem presents a game played between Alice and Bob on a line of colored pieces, represented by the string colors. Each piece is colored either \'A\' or \'B\', and the players take alternating turns to remove pieces according to certain rules. Alice can remove a piece \'A\' if both its neighbors are also \'A\', and Bob can remove a piece \'B\' if both its neighbors are also \'B\'. The game continues until a player cannot make a valid move, and the one who cannot move loses the game. The task is to determine if Alice will win or if Bob will win when both play optimally.\n\n---\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize two variables, aliceWins and bobWins, to keep track of the number of consecutive valid moves each player can make.\n\n2. Iterate through the string colors from the second character to the second-to-last character. This ensures that we do not consider the edge pieces (the first and last characters), as the rules for removal require pieces to have both neighbors.\n\n3. For each position i, check if there are three consecutive \'A\'s or three consecutive \'B\'s. If there are, increment the corresponding counter (aliceWins or bobWins). This means that Alice can remove the \'A\' piece or Bob can remove the \'B\' piece at that position.\n\n4. After iterating through the string, compare the counts of aliceWins and bobWins. If aliceWins is greater than bobWins, Alice wins the game, so return true. Otherwise, Bob wins the game, so return false.\n\n---\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe time complexity of this code is O(n), where n is the length of the input string colors. This is because we iterate through the string once, visiting each character once.\n\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity is O(1) because we only use a constant amount of extra space to store the two counters aliceWins and bobWins. Regardless of the size of the input string, the space required remains constant.\n\n---\n\n# Code\n```\nclass Solution {\npublic:\n bool winnerOfGame(string colors) {\n int n = colors.length();\n int aliceWins = 0, bobWins = 0;\n \n // Count the number of consecutive \'A\'s and \'B\'s.\n for (int i = 1; i < n - 1; ++i) {\n if (colors[i - 1] == \'A\' && colors[i] == \'A\' && colors[i + 1] == \'A\') {\n aliceWins++;\n } else if (colors[i - 1] == \'B\' && colors[i] == \'B\' && colors[i + 1] == \'B\') {\n bobWins++;\n }\n }\n \n // Alice starts, so if she has more opportunities to remove \'A\', she wins.\n if (aliceWins > bobWins) {\n return true;\n }\n \n return false;\n }\n};\n\n``` | 5 | 1 | ['Math', 'String', 'Dynamic Programming', 'Greedy', 'C', 'Game Theory', 'Python', 'C++', 'Java', 'Python3'] | 1 |
remove-colored-pieces-if-both-neighbors-are-the-same-color | ✅ O(n) Solution with detailed explanation ever - Beats 100% with runtime and memory acceptance 😎 | on-solution-with-detailed-explanation-ev-rs0h | Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem asks us to determine the winner of a game played by Alice and Bob based on | nikkipriya_78 | NORMAL | 2023-10-02T06:56:11.684970+00:00 | 2023-10-02T06:56:11.685004+00:00 | 282 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem asks us to determine the winner of a game played by Alice and Bob based on certain rules about removing pieces from a string. Alice can only remove \'A\' pieces if both neighbors are also \'A\', and Bob can only remove \'B\' pieces if both neighbors are \'B\'. They take alternating turns, and the player who cannot make a move loses the game.\n\n---\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize two variables, aliceWins and bobWins, to keep track of the count of consecutive valid moves each player can make.\n\n2. Iterate through the string colors from the second character to the second-to-last character (indices from 1 to n - 2).\n\n3. For each position i, check if there are three consecutive \'A\'s or three consecutive \'B\'s. If yes, increment the corresponding counter (aliceWins or bobWins).\n\n4. After iterating through the string, compare the counts of aliceWins and bobWins. If aliceWins is greater than bobWins, Alice wins the game, so return true; otherwise, Bob wins the game, so return false.\n\n---\n\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe time complexity of this code is O(n), where n is the length of the input string colors. This is because we iterate through the string once.\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity is O(1) because we only use a constant amount of extra space to store aliceWins and bobWins, regardless of the size of the input string.\n\n---\n# Do upvote if you like the solution and explanation please \uD83D\uDE04\n# Code\n```\nclass Solution {\npublic:\n bool winnerOfGame(string colors) {\n int n = colors.length();\n int aliceWins = 0, bobWins = 0;\n \n // Iterate through the string colors.\n for (int i = 1; i < n - 1; ++i) {\n if (colors[i - 1] == \'A\' && colors[i] == \'A\' && colors[i + 1] == \'A\') {\n aliceWins++;\n } else if (colors[i - 1] == \'B\' && colors[i] == \'B\' && colors[i + 1] == \'B\') {\n bobWins++;\n }\n }\n \n // Alice starts, so if she has more opportunities to remove \'A\', she wins.\n if (aliceWins > bobWins) {\n return true;\n }\n \n return false;\n }\n};\n\n``` | 5 | 1 | ['Math', 'Two Pointers', 'String', 'Greedy', 'C', 'Game Theory', 'Python', 'C++', 'Java', 'Python3'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.