Easy to understand O(n) time and O(1) space solution
The key is to realize alternating binary string can ONLY be either 101010... or 010101....  We can compare the string with both and calculate the number of type
The key is to realize alternating binary string can ONLY be either 101010... or 010101....  We can compare the string with both and calculate the number of type2 operations.  Instead of comparing one digit by one digit, count the number of ones at odd indexes and ones at even indexes, then with simple calculation we can come up with how many flips (type2 operations) needed.    \n\nSince type1 operation is allowed too, we can enumerate all possible number of type1 operations and count the number of types operations. The smallest count will be the answer.  The tricky part is, if the length of s is odd, when performing type1 operation, the first number moved to the end remains at even index.\n\n```\nclass Solution:\n    def minFlips(self, s: str) -> int:\n        ret = len(s)\n        onesOdd = 0 \n        onesEven = 0\n        for i, digit in enumerate(s):\n            if i % 2 == 0 and digit == "1":\n                onesEven += 1\n            elif i % 2 == 1 and digit == "1":\n                onesOdd += 1\n                    \n        total = onesEven + onesOdd\n        for i in range(len(s)):\n           \n            #target: 010101...\n            flips = onesEven + (len(s)//2 - onesOdd)\n            ret = min(ret, flips)\n            \n            #target: 101010...\n            flips = (len(s)-len(s)//2 - onesEven) + onesOdd\n            ret = min(ret, flips)\n            \n            if len(s) % 2 == 0:\n                break  \n            else:\n                onesEven = onesOdd + (1 if s[i] == "1" else 0)\n                onesOdd = total - onesEven\n        return ret\n            \n```
['Python3']