✅☑[C++] || O(n) || Easiest Solutions || EXPLAINED🔥
\n\n\n# PLEASE UPVOTE IF IT HELPED\n\n---\n\n# Approach\n(Also explained in the code)\n1. The function winnerOfGame takes a string colors as input, which repres
\n\n\n# *PLEASE UPVOTE IF IT HELPED*\n\n---\n\n# Approach\n***(Also explained in the code)***\n1. The function `winnerOfGame` takes a string `colors` as input, which represents a sequence of colors played in a game.\n\n1. It initializes `a` and `b` to zero. These variables are used to count the consecutive triplets of \'A\' and \'B\' colors, respectively.\n\n1. The loop iterates through the string starting from the second character (index 1) up to the second-to-last character (`index n - 2`).\n\n1. Inside the loop, it checks if there is a consecutive triplet of the same color. If so, it increments the count for the respective player (Alice or Bob).\n\n1. Finally, the function returns `true` if Alice (A) has more consecutive triplets than Bob (B), indicating that Alice wins; otherwise, it returns `false`, indicating that Bob wins.\n---\n\n# Complexity\n- **Time complexity:**\n$$O(n)$$\n\n- **Space complexity:**\n$$O(1)$$\n\n---\n\n# Code\n```\nclass Solution {\npublic:\n    bool winnerOfGame(string colors) {\n        int n = colors.size();\n\n        if (n < 3) return false; // If the length of the string is less than 3, Bob wins.\n\n        int a = 0; // Initialize the count of consecutive triplets for A\n        int b = 0; // Initialize the count of consecutive triplets for B\n\n        // Iterate through the string starting from the second character\n        for (int i = 1; i < n - 1; i++) {\n            // Check if there is a consecutive triplet (e.g., "AAA" or "BBB")\n            if (colors[i] == colors[i - 1] && colors[i] == colors[i + 1]) {\n                if (colors[i] == \'A\') a++; // Count consecutive triplets for A\n                else if (colors[i] == \'B\') b++; // Count consecutive triplets for B\n            }\n        }\n\n        return a > b; // Alice wins if she has more consecutive triplets than Bob\n    }\n};\n\n\n\n```\n\n# *PLEASE UPVOTE IF IT HELPED*\n\n---\n\n---\n\n\n\n
['Math', 'String', 'Greedy', 'Game Theory', 'C++']