🔥Beats 100% | ✅ Line by Line Expl. | [PY/Java/C++/C#/C/JS/Rust/Go]
python []\nclass Solution:\n    def winnerOfGame(self, colors: str) -> bool:\n        totalA = 0  # Initialize a variable to store the total points of player A.
```python []\nclass Solution:\n    def winnerOfGame(self, colors: str) -> bool:\n        totalA = 0  # Initialize a variable to store the total points of player A.\n        totalB = 0  # Initialize a variable to store the total points of player B.\n        currA = 0   # Initialize a variable to count the current consecutive colors of A.\n        currB = 0   # Initialize a variable to count the current consecutive colors of B.\n\n        # Iterate through the characters in the \'colors\' string.\n        for char in colors:\n            if char == \'A\':    # If the current character is \'A\':\n                currA += 1     # Increment the count of consecutive \'A\' colors.\n                if currB > 2:  # If there were more than 2 consecutive \'B\' colors before this \'A\':\n                    totalB += currB - 2  # Add the excess consecutive \'B\' colors to totalB.\n                currB = 0       # Reset the consecutive \'B\' count since there\'s an \'A\'.\n            else:              # If the current character is \'B\':\n                currB += 1     # Increment the count of consecutive \'B\' colors.\n                if currA > 2:  # If there were more than 2 consecutive \'A\' colors before this \'B\':\n                    totalA += currA - 2  # Add the excess consecutive \'A\' colors to totalA.\n                currA = 0       # Reset the consecutive \'A\' count since there\'s a \'B\'.\n\n        # After the loop, add any remaining consecutive \'A\' and \'B\' colors to their respective totals.\n        if currA > 2:\n            totalA += currA - 2\n        if currB > 2:\n            totalB += currB - 2\n\n        # Compare the total points for \'A\' and \'B\' to determine the winner.\n        return totalA > totalB  # If \'A\' has more points, return True (A wins); otherwise, return False (B wins or it\'s a tie).\n\n```\n```Java []\npublic class Solution {\n    public boolean winnerOfGame(String colors) {\n        int totalA = 0;  // Initialize a variable to store the total points of player A.\n        int totalB = 0;  // Initialize a variable to store the total points of player B.\n        int currA = 0;   // Initialize a variable to count the current consecutive colors of A.\n        int currB = 0;   // Initialize a variable to count the current consecutive colors of B.\n\n        // Iterate through the characters in the \'colors\' string.\n        for (char c : colors.toCharArray()) {\n            if (c == \'A\') {    // If the current character is \'A\':\n                currA++;       // Increment the count of consecutive \'A\' colors.\n                if (currB > 2)  // If there were more than 2 consecutive \'B\' colors before this \'A\':\n                    totalB += currB - 2;  // Add the excess consecutive \'B\' colors to totalB.\n                currB = 0;       // Reset the consecutive \'B\' count since there\'s an \'A\'.\n            } else {           // If the current character is \'B\':\n                currB++;       // Increment the count of consecutive \'B\' colors.\n                if (currA > 2)  // If there were more than 2 consecutive \'A\' colors before this \'B\':\n                    totalA += currA - 2;  // Add the excess consecutive \'A\' colors to totalA.\n                currA = 0;       // Reset the consecutive \'A\' count since there\'s a \'B\'.\n            }\n        }\n\n        // After the loop, add any remaining consecutive \'A\' and \'B\' colors to their respective totals.\n        if (currA > 2)\n            totalA += currA - 2;\n        if (currB > 2)\n            totalB += currB - 2;\n\n        // Compare the total points for \'A\' and \'B\' to determine the winner.\n        return totalA > totalB;  // If \'A\' has more points, return true (A wins); otherwise, return false (B wins or it\'s a tie).\n    }\n}\n\n```\n```C++ []\nclass Solution {\npublic:\n    bool winnerOfGame(string colors) {\n        int totalA = 0, totalB = 0;  // Initialize counters for the total points of players A and B.\n        int currA = 0, currB = 0;    // Initialize counters for the current consecutive colors of A and B.\n\n        // Iterate through the characters in the \'colors\' string.\n        for (int i = 0; i < colors.size(); i++) {\n            if (colors[i] == \'A\') {   // If the current character is \'A\':\n                currA++;              // Increment the count of consecutive \'A\' colors.\n                if (currB > 2)        // If there were more than 2 consecutive \'B\' colors before this \'A\':\n                    totalB += currB - 2;  // Add the excess consecutive \'B\' colors to totalB.\n                currB = 0;            // Reset the consecutive \'B\' count since there\'s an \'A\'.\n            } else {                 // If the current character is \'B\':\n                currB++;              // Increment the count of consecutive \'B\' colors.\n                if (currA > 2)        // If there were more than 2 consecutive \'A\' colors before this \'B\':\n                    totalA += currA - 2;  // Add the excess consecutive \'A\' colors to totalA.\n                currA = 0;            // Reset the consecutive \'A\' count since there\'s a \'B\'.\n            }\n        }\n\n        // After the loop, add any remaining consecutive \'A\' and \'B\' colors to their respective totals.\n        if (currA > 2)\n            totalA += currA - 2;\n        if (currB > 2)\n            totalB += currB - 2;\n\n        // Compare the total points for \'A\' and \'B\' to determine the winner.\n        if (totalA > totalB)\n            return true;  // If \'A\' has more points, return true (A wins).\n        return false;     // Otherwise, return false (B wins or it\'s a tie).\n    }\n};\n\n```\n```C# []\npublic class Solution {\n    public bool WinnerOfGame(string colors) {\n        int totalA = 0;  // Initialize a variable to store the total points of player A.\n        int totalB = 0;  // Initialize a variable to store the total points of player B.\n        int currA = 0;   // Initialize a variable to count the current consecutive colors of A.\n        int currB = 0;   // Initialize a variable to count the current consecutive colors of B.\n\n        // Iterate through the characters in the \'colors\' string.\n        foreach (char c in colors) {\n            if (c == \'A\') {    // If the current character is \'A\':\n                currA++;       // Increment the count of consecutive \'A\' colors.\n                if (currB > 2)  // If there were more than 2 consecutive \'B\' colors before this \'A\':\n                    totalB += currB - 2;  // Add the excess consecutive \'B\' colors to totalB.\n                currB = 0;       // Reset the consecutive \'B\' count since there\'s an \'A\'.\n            } else {           // If the current character is \'B\':\n                currB++;       // Increment the count of consecutive \'B\' colors.\n                if (currA > 2)  // If there were more than 2 consecutive \'A\' colors before this \'B\':\n                    totalA += currA - 2;  // Add the excess consecutive \'A\' colors to totalA.\n                currA = 0;       // Reset the consecutive \'A\' count since there\'s a \'B\'.\n            }\n        }\n\n        // After the loop, add any remaining consecutive \'A\' and \'B\' colors to their respective totals.\n        if (currA > 2)\n            totalA += currA - 2;\n        if (currB > 2)\n            totalB += currB - 2;\n\n        // Compare the total points for \'A\' and \'B\' to determine the winner.\n        return totalA > totalB;  // If \'A\' has more points, return true (A wins); otherwise, return false (B wins or it\'s a tie).\n    }\n}\n\n```\n```C []\n#include <stdbool.h>  // Include the header for boolean data type.\n\nbool winnerOfGame(char *colors) {\n    int totalA = 0;  // Initialize a variable to store the total points of player A.\n    int totalB = 0;  // Initialize a variable to store the total points of player B.\n    int currA = 0;   // Initialize a variable to count the current consecutive colors of A.\n    int currB = 0;   // Initialize a variable to count the current consecutive colors of B.\n\n    // Iterate through the characters in the \'colors\' string.\n    for (int i = 0; colors[i] != \'\\0\'; i++) {\n        char currentChar = colors[i];\n        if (currentChar == \'A\') {  // If the current character is \'A\':\n            currA++;              // Increment the count of consecutive \'A\' colors.\n            if (currB > 2)        // If there were more than 2 consecutive \'B\' colors before this \'A\':\n                totalB += currB - 2;  // Add the excess consecutive \'B\' colors to totalB.\n            currB = 0;            // Reset the consecutive \'B\' count since there\'s an \'A\'.\n        } else {                   // If the current character is \'B\':\n            currB++;              // Increment the count of consecutive \'B\' colors.\n            if (currA > 2)        // If there were more than 2 consecutive \'A\' colors before this \'B\':\n                totalA += currA - 2;  // Add the excess consecutive \'A\' colors to totalA.\n            currA = 0;            // Reset the consecutive \'A\' count since there\'s a \'B\'.\n        }\n    }\n\n    // After the loop, add any remaining consecutive \'A\' and \'B\' colors to their respective totals.\n    if (currA > 2)\n        totalA += currA - 2;\n    if (currB > 2)\n        totalB += currB - 2;\n\n    // Compare the total points for \'A\' and \'B\' to determine the winner.\n    return totalA > totalB;  // If \'A\' has more points, return true (A wins); otherwise, return false (B wins or it\'s a tie).\n}\n\n```\n```JavaScript []\nclass Solution {\n    winnerOfGame(colors) {\n        let totalA = 0;  // Initialize a variable to store the total points of player A.\n        let totalB = 0;  // Initialize a variable to store the total points of player B.\n        let currA = 0;   // Initialize a variable to count the current consecutive colors of A.\n        let currB = 0;   // Initialize a variable to count the current consecutive colors of B.\n\n        // Iterate through the characters in the \'colors\' string.\n        for (let i = 0; i < colors.length; i++) {\n            const char = colors[i];\n            if (char === \'A\') {    // If the current character is \'A\':\n                currA++;           // Increment the count of consecutive \'A\' colors.\n                if (currB > 2) {   // If there were more than 2 consecutive \'B\' colors before this \'A\':\n                    totalB += currB - 2;  // Add the excess consecutive \'B\' colors to totalB.\n                }\n                currB = 0;          // Reset the consecutive \'B\' count since there\'s an \'A\'.\n            } else {               // If the current character is \'B\':\n                currB++;           // Increment the count of consecutive \'B\' colors.\n                if (currA > 2) {   // If there were more than 2 consecutive \'A\' colors before this \'B\':\n                    totalA += currA - 2;  // Add the excess consecutive \'A\' colors to totalA.\n                }\n                currA = 0;          // Reset the consecutive \'A\' count since there\'s a \'B\'.\n            }\n        }\n\n        // After the loop, add any remaining consecutive \'A\' and \'B\' colors to their respective totals.\n        if (currA > 2) {\n            totalA += currA - 2;\n        }\n        if (currB > 2) {\n            totalB += currB - 2;\n        }\n\n        // Compare the total points for \'A\' and \'B\' to determine the winner.\n        return totalA > totalB;  // If \'A\' has more points, return true (A wins); otherwise, return false (B wins or it\'s a tie).\n    }\n}\n\n```\n```Rust []\nstruct Solution;\n\nimpl Solution {\n    pub fn winner_of_game(colors: &str) -> bool {\n        let mut total_a = 0;  // Initialize a variable to store the total points of player A.\n        let mut total_b = 0;  // Initialize a variable to store the total points of player B.\n        let mut curr_a = 0;   // Initialize a variable to count the current consecutive colors of A.\n        let mut curr_b = 0;   // Initialize a variable to count the current consecutive colors of B.\n\n        // Iterate through the characters in the \'colors\' string.\n        for char in colors.chars() {\n            if char == \'A\' {    // If the current character is \'A\':\n                curr_a += 1;     // Increment the count of consecutive \'A\' colors.\n                if curr_b > 2 {  // If there were more than 2 consecutive \'B\' colors before this \'A\':\n                    total_b += curr_b - 2;  // Add the excess consecutive \'B\' colors to total_b.\n                }\n                curr_b = 0;       // Reset the consecutive \'B\' count since there\'s an \'A\'.\n            } else {              // If the current character is \'B\':\n                curr_b += 1;     // Increment the count of consecutive \'B\' colors.\n                if curr_a > 2 {  // If there were more than 2 consecutive \'A\' colors before this \'B\':\n                    total_a += curr_a - 2;  // Add the excess consecutive \'A\' colors to total_a.\n                }\n                curr_a = 0;       // Reset the consecutive \'A\' count since there\'s a \'B\'.\n            }\n        }\n\n        // After the loop, add any remaining consecutive \'A\' and \'B\' colors to their respective totals.\n        if curr_a > 2 {\n            total_a += curr_a - 2;\n        }\n        if curr_b > 2 {\n            total_b += curr_b - 2;\n        }\n\n        // Compare the total points for \'A\' and \'B\' to determine the winner.\n        total_a > total_b  // If \'A\' has more points, return true (A wins); otherwise, return false (B wins or it\'s a tie).\n    }\n}\n\nfn main() {\n    let colors = "AABBBBAAABBAA";  // Example input string.\n    let result = Solution::winner_of_game(colors);\n    println!("Is player A the winner? {}", result);\n}\n\n```\n```Go []\npackage main\n\nimport "fmt"\n\nfunc winnerOfGame(colors string) bool {\n\ttotalA := 0 // Initialize a variable to store the total points of player A.\n\ttotalB := 0 // Initialize a variable to store the total points of player B.\n\tcurrA := 0  // Initialize a variable to count the current consecutive colors of A.\n\tcurrB := 0  // Initialize a variable to count the current consecutive colors of B.\n\n\t// Iterate through the characters in the \'colors\' string.\n\tfor _, char := range colors {\n\t\tif char == \'A\' { // If the current character is \'A\':\n\t\t\tcurrA++ // Increment the count of consecutive \'A\' colors.\n\t\t\tif currB > 2 { // If there were more than 2 consecutive \'B\' colors before this \'A\':\n\t\t\t\ttotalB += currB - 2 // Add the excess consecutive \'B\' colors to totalB.\n\t\t\t}\n\t\t\tcurrB = 0 // Reset the consecutive \'B\' count since there\'s an \'A\'.\n\t\t} else { // If the current character is \'B\':\n\t\t\tcurrB++ // Increment the count of consecutive \'B\' colors.\n\t\t\tif currA > 2 { // If there were more than 2 consecutive \'A\' colors before this \'B\':\n\t\t\t\ttotalA += currA - 2 // Add the excess consecutive \'A\' colors to totalA.\n\t\t\t}\n\t\t\tcurrA = 0 // Reset the consecutive \'A\' count since there\'s a \'B\'.\n\t\t}\n\t}\n\n\t// After the loop, add any remaining consecutive \'A\' and \'B\' colors to their respective totals.\n\tif currA > 2 {\n\t\ttotalA += currA - 2\n\t}\n\tif currB > 2 {\n\t\ttotalB += currB - 2\n\t}\n\n\t// Compare the total points for \'A\' and \'B\' to determine the winner.\n\treturn totalA > totalB // If \'A\' has more points, return true (A wins); otherwise, return false (B wins or it\'s a tie).\n}\n\nfunc main() {\n\tcolors := "AAABBB"\n\tresult := winnerOfGame(colors)\n\tfmt.Println(result) // Output: true (A wins)\n}\n\n```
['C', 'C++', 'Java', 'Go', 'Python3', 'Rust', 'JavaScript', 'C#']