JAVA || O(N) Time O(1) Space || Faster || Easiest Approach || Easy To Understand
\n\n# YouTube Video:\nhttps://youtu.be/8vnMbZ_LBR0\nPlease subscribe to my YouTube Channel.\n# Intuition\n- To determine the winner, we need to count how many s
![Screenshot 2023-10-02 093423.png](https://assets.leetcode.com/users/images/bc9315dc-3ed3-4956-a8c4-2f431cfe658e_1696220205.5716782.png)\n\n# YouTube Video:\n[https://youtu.be/8vnMbZ_LBR0]()\nPlease subscribe to my YouTube Channel.\n# Intuition\n- To determine the winner, we need to count how many sequences of consecutive \'A\'s and \'B\'s are present in the given string, where each sequence has a length of 3 or more. The reason is that if a player has three consecutive pieces they can remove, they will continue doing so on their turn until they can\'t. The player with more such sequences has the advantage.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialize variables `aCnt` and `bCnt` to `0` to keep track of the count of sequences for Alice and Bob, respectively.\n2. Initialize temporary variables `aTemp` and `bTemp` to `0` to keep track of consecutive \'A\' and \'B\' pieces encountered during the traversal.\n3. Iterate through the characters of the input string `colors`.\n    - If the current character is `\'A\'`, reset `bTemp` to `0` and increment `aTemp`. If `aTemp` becomes 3 or more, increment `aCnt`.\n    - If the current character is   `\'B\'`, reset `aTemp` to `0` and increment `bTemp`. If `bTemp` becomes 3 or more, increment `bCnt`.\n4. After processing the entire string, compare `aCnt` and `bCnt` to determine the winner. If `aCnt` is greater than `bCnt`, Alice wins; otherwise, Bob wins.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: `O(N)`, where `N` is the length of the input string colors. We iterate through the string once.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: `O(1)`, as we use a constant amount of extra space for variables.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public boolean winnerOfGame(String colors) {\n        int aCnt=0, bCnt=0;\n        int aTemp=0, bTemp=0;\n\n        for(int i=0;i<colors.length();i++)\n        {\n            char c = colors.charAt(i);\n            if(c==\'A\')\n            {\n                bTemp=0;\n                aTemp+=1;\n                if(aTemp>=3)\n                {\n                    aCnt+=1;\n                }\n            }\n            else\n            {\n                aTemp=0;\n                bTemp+=1;\n                if(bTemp>=3)\n                {\n                    bCnt+=1;\n                }\n            }\n        }\n        return aCnt>bCnt;\n    }\n}\n```\n![upvote.png](https://assets.leetcode.com/users/images/64dc109a-6502-42b6-ba15-2eeb10511f8e_1696228037.1849086.png)\n
['String', 'Java']