Beats 100%||C/C++/Python find AAA... & BBB...
Intuition\n Describe your first thoughts on how to solve this problem. \nThis is a game. Find the substring \'AAA....\' and \'BBB....\'.\n\nLet\'s see some case
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis is a game. Find the substring \'AAA....\' and \'BBB....\'.\n\nLet\'s see some cases (number of Alice can take vs number of Bob can take )\n```\nAAABABB    ->1 vs 0 result:1\nAA         ->0 vs 0 result:0\nABBBBBBBAAA->1 vs 5 result:0\nAAAABBBB   ->2 vs 2 result:0\n\n```\n[Please turn English subtitles if necessary]\n[https://youtu.be/heg4EVIqfRE?si=2gS9VCNvYSN-CWnw](https://youtu.be/heg4EVIqfRE?si=2gS9VCNvYSN-CWnw)\n\n2nd C++ approach uses substr beats 100%\n# Approach\n<!-- Describe your approach to solving the problem. -->\nIf there is substring \'AAA...A\' with n \'A\' (n>2) which means Alice can take (n-2) \'A\' from this substring. The solving question becomes just finding the substring \'AAA....\' and \'BBB....\'!\n\n1. `int player[2] = {0};` initializes an integer array player with two elements, both initialized to zero. This array is used to keep track of the scores of two players, where index 0 represents Alice and index 1 represents Bob.\n\n2. `int n = colors.size();` calculates the length of the input string colors.\n\n3. `int len = 1;` initializes an integer variable len to 1. This variable is used to keep track of the current streak of consecutive colors.\n\n4. `char prev = colors[0];` initializes a character variable prev with the first character of the input string colors. This variable is used to keep track of the previously encountered color.\n\n5. The `#pragma unroll` directive is a compiler hint, but in this context, it doesn\'t have any effect on the code\'s behavior.\n\nThe code then enters a for loop that iterates through the characters of the colors string starting from the second character (index 1).\n\n6. Inside the loop:\n`char c = colors[i];` assigns the current character in the loop to the variable c.\n`if (prev == c) len++;` checks if the current character c is the same as the previous character prev. If they are the same, it increments the len variable to track the length of the current streak.\nelse len = 1; if the current character is different from the previous one, it resets the len variable to 1, indicating the start of a new streak.\n`if (len > 2) player[c - \'A\']++;` checks if the streak length len is greater than 2. If so, it increments the score of the player corresponding to the current character c. The subtraction of \'A\' from c converts the character to an integer index (e.g., \'A\' becomes 0, \'B\' becomes 1).\n`prev = c;` updates the prev variable to the current character for the next iteration.\n\n7. Finally, the function returns `player[0] > player[1];`, which compares the scores of Alice and Bob. If Alice\'s score is greater than Bob\'s score, it returns true, indicating that Alice is the winner; otherwise, it returns false, indicating that Bob is the winner.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n $$O(n)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n $$O(1)$$\n# Code : C code runtime 8ms, C++ runtime 16 ms Beats 99.76%\n```C []\nbool winnerOfGame(char * colors){\n    int player[2]={0};// 0 for Alice, 1 for Bob\n    int n=strlen(colors);\n    int len=1;\n    char prev=colors[0];\n\n    #pragma unroll\n    for(register int i=1; i<n; i++ ){\n        char c=colors[i];\n        if (prev==c) len++;\n        else len=1;\n        if (len>2) player[c-\'A\']++;\n        prev=c;\n    } \n    return player[0]>player[1];\n\n}\n```\n```C++ []\n\nclass Solution {\npublic:\n    bool winnerOfGame(string colors) {\n        int player[2]={0};// 0 for Alice, 1 for Bob\n        int n=colors.size();\n        int len=1;\n        char prev=colors[0];\n        #pragma unroll\n        for(int i=1; i<n; i++){\n            char c=colors[i];\n            if (prev==c) len++;\n            else len=1;\n            if (len>2) player[c-\'A\']++;\n            prev=c;\n        } \n    //    cout<<player[0]<<" vs "<<player[1]<<endl;\n        return player[0]>player[1];\n    }\n};\n```\n# Python solution\n```\nclass Solution:\n    def winnerOfGame(self, colors: str) -> bool:\n        player=[0, 0]\n        len=1\n        prev=colors[0]\n        for c in colors[1:]:\n            if prev==c: len+=1\n            else: len=1\n            if len>2: \n                player[ord(c)-ord(\'A\')]+=1\n            prev=c\n        return player[0]>player[1]\n        \n```\n# C++ other approach runtime 12 ms Beats 100%\n```\nclass Solution {\npublic:\n  bool winnerOfGame(string colors) {\n    int player[2]={0};\n    int n=colors.size();\n    if (n<3) return 0;//Be careful for edge cases\n    int prev2=colors[0], prev1=colors[1];\n\n    #pragma unroll\n    for(char& c: colors.substr(2)) {\n        if (c==prev1 && c==prev2) player[c-\'A\']++;\n        prev2=prev1, prev1=c;\n    }\n//    cout<<player[0]<<" vs "<<player[1]<<endl;\n    return player[0]>player[1];\n    }\n}\n```\n
['String', 'Game Theory']