✅ O(n) Solution with detailed explanation ever - Beats 100% with runtime and memory acceptance 😎
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 
# 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```
['Math', 'Two Pointers', 'String', 'Greedy', 'C', 'Game Theory', 'Python', 'C++', 'Java', 'Python3']