Beats 96.22% in speed || Two Pointer Approach || Very Short
Intuition\nThe code appears to be implementing a function winnerOfGame that takes a string colors as input. This function aims to determine the winner of a game
# Intuition\nThe code appears to be implementing a function winnerOfGame that takes a string colors as input. This function aims to determine the winner of a game based on certain rules related to consecutive color sequences.\n\n# Approach\nThe code uses a while loop to iterate over the characters of the colors string. Within this loop, there is another while loop that starts at the current position i and continues until it encounters a different color. This inner loop helps in finding consecutive sequences of the same color.\n\nInside the inner loop, it calculates the length of the consecutive sequence (turn). If this length is greater than or equal to 3, it subtracts 2 from it (as per the game rules), otherwise, it sets turn to 0.\n\nThe code then updates the score of player A or player B based on the current color and the calculated turn value.\n\nFinally, after processing the entire string, it checks whether the score of player A (a) is greater than player B (b) and returns the result.\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\npublic:\n    bool winnerOfGame(string colors) {\n        int a = 0, b = 0, j = 0, i = 0;\n        while(i < colors.size()) {\n            j = i;\n            while (j < colors.size() && colors[j] == colors[i]) j++;\n            int turn = (j - i) >= 3 ? (j - i) - 2 : 0;\n            colors[i] == \'A\' ? a += turn : b += turn;\n            i = j;\n        }\n        return a > b;\n    }\n};\n```
['Two Pointers', 'String', 'Counting', 'Game Theory', 'C++']