Detail Explaination, Easy to read, commented C++ solution O(n) time, O(1) space
\n// Idea:\n//   The strategy of coming up with the below solution comes from a few observation:\n//\n//      1. Alternating sequence comes in two forms, being 
```\n// Idea:\n//   The strategy of coming up with the below solution comes from a few observation:\n//\n//      1. Alternating sequence comes in two forms, being 0101 sequence or 1010 sequence\n//\n//      2. Sum of number of bits you need to flip to satisfy 0101 seq and 1010 seq is the input size.\n//         The reason being each bit that satisfy 0101 seq does not satisfy 1010.\n//\n//      3. Shifting the string can be interpreted as shifting to the alternating sequence we are\n//         comparing the string with to the opposite direction.\n//\n//         Example:\n//              before              left shift seq by 1 bit  compensate by right shifting both str and seq\n//              str: 1 1 1 0        str: 1 1 1 0             str: 0 1 1 1\n//              seq: 0 1 0 1 (3)    seq: 1 0 1 0 (1)         seq: 0 1 0 1 (1)\n//              seq: 1 0 1 0 (1)    seq: 0 1 0 1 (3)         seq: 1 0 1 0 (3)\n//\n//         Note that the number of bits to flip is str.size() - bits to flip in 0101 sequence (refer observation 2).\n//         So as a form of optimization, we only need to care about one of the two sequence, where I chose 0101.\n//\n//      4. The first obvious observation is that if the string is even, shifting will not contribute\n//         to the reduction of bits we need to alter, because you don\'t get less bits to alter if you\n//         only alter the sequence.\n//\n//      5. By 4, the logical step is to think where we can gain less bits to flip by shifting.\n//         The answer is when the string has odd length. Let\'s take a look at an example.\n//\n//         str: 1 1 1 0 0\n//         seq: 0 1 0 1 0 (3)\n//\n//         When right shift by 1 bit, observe the alternating sequence\n//         str: 1 1 1 0 0      compensate     str: 0 1 1 1 0\n//         seq: 1 0 1 0 0 (1)      ->         seq: 0 1 0 1 0 (1)\n//\n//         As you can see, we have gain two bits in this case. Why?\n//         We can split our string into 2 parts, where the second part is the last bit.\n//         \n//         str (part 1): 1 1 1 0     str (part 2): 0\n//         seq         : 0 1 0 1     seq         : 0\n//\n//         For part 1, a shift will simply change the number of bits to count to (str.size() - bits to flip)\n//         For part 2, after shift, the sequence mask will still be 0. If the last bit is 0, we deduct 1 to the\n//         part 1 result (1 less bit to flip).\n//\n//         Rewriting the equations with conditions:\n//\n//         if (part2 == 0) {\n//             bitsToFlip = (s.size() - part1ToFlip) - 1;\n//         }\n//\n//         if (part2 == 1) {\n//             bitsToFlip = (s.size() - part1ToFlip) + 1;\n//         }\n//\n// Complexity Analysis:\n//    O(n) time, O(1) space\n\nclass Solution {\npublic:\n    int minFlips(string s) {\n        int bitsToFlip = 0; // required bit change if alternating seq is 010...01\n        int minFlip = INT_MAX, maxFlip = INT_MIN;\n        \n        // Get number of bits we need to flip if we comapre to 010101 alternating sequence\n        char expectedC = \'0\';\n        for (char &c : s) {\n            if (c != expectedC) {\n                bitsToFlip++;\n            }\n            expectedC = expectedC == \'0\' ? \'1\' : \'0\';\n        }\n        \n        // If the string has an even length, shifting does not affect the outcome\n        if (!(s.size() & 1)) return min(bitsToFlip, (int)(s.size() - bitsToFlip));\n        \n        // Get Min / Max of bits to flip if string has odd length\n        for (int i = s.size() - 1; i >= 0; i--) {\n            if (s[i] == \'0\') {\n                bitsToFlip = s.size() - bitsToFlip - 1;\n            } else if (s[i] == \'1\') {\n                bitsToFlip = s.size() - bitsToFlip + 1;\n            }\n            minFlip = min(minFlip, bitsToFlip);\n            maxFlip = max(maxFlip, bitsToFlip);\n        }\n        \n        return min(minFlip, (int)(s.size() - maxFlip)); // consider the 2 altering sequence\n    }\n};\n```
['C', 'C++']