🚀Count Solution || Explained Intuition || Commented Code🚀
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
# 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![leet_sol.jpg](https://assets.leetcode.com/users/images/644c2d1c-5b0e-445d-883c-16f4cc0143b0_1696206136.0121193.jpeg)\n\n
['String', 'Python', 'C++', 'Java', 'Python3']