[Python] O(N) time O(1) space solution with thinking process
Two important observations in solving this problem:\n1. Assuming we cannot shift the characters, what would be the answer?\n    To make the result an alternatin
Two important observations in solving this problem:\n1. Assuming we cannot shift the characters, what would be the answer?\n    To make the result an alternating string, it would be either \'010101...\' or \'101010...\', then all \'1\' must be at odd indices or even indices.\n\tWe can count the number of \'1\' at odd indices and even indices initially, say the numbers are \'odd\' and \'even\', and assuming the length of input string s is n.\n\tIf n is even, then finally we either need to have n / 2 \'1\' at odd indices or even indices, the minimal number of flip would be min(n // 2 - odd + even, n // 2 - even + odd)\n\tIf n is odd, if we finally have all \'1\' at odd indices, the number of flip is (n - 1) // 2 - odd + even, otherwise, it is (n + 1) // 2 - even + odd, so the minimal is min((n - 1) // 2 - odd + even, (n + 1) // 2 - even + odd)\n2. Now if we can shift the characters, how does it affect the above calculation process?\n    We need to think about how the shifting may change the number of \'1\' at odd indices and even indices\n    When the first character is shifted to the end, its index changes from 0 to n - 1, notice that if n is even, n - 1 would be odd, meaning it is flipping, which is aligned with the change for all the rest characters, so in the whole shifting process, the number of \'1\' at odd indices and even indices are always exchanging, meaning odd, even = even, odd, as finally we are targeting at n / 2 \'1\' at odd indices or even indices, the exchange between odd and even during the shifting process does the affect the final result\n\tHowever, if n is odd, n - 1 would be even, then when we shift the first character to the end, its index keeps as even, then if the first character is \'0\', the change on number of \'1\' at odd indices and even indices is same as before, meaning odd, even = even, odd, but if the first character is \'1\', the change would be odd, even = even - 1, odd + 1\n```\nclass Solution:\n    def minFlips(self, s: str) -> int:\n        n = len(s)\n        odd = even = 0\n        for i, ch in enumerate(s):\n            if ch == \'0\':\n                continue\n            \n            if i % 2 == 1:\n                odd += 1\n            else:\n                even += 1\n        \n        if n % 2 == 0:\n            res = min(n // 2 - odd + even, n // 2 - even + odd)\n        else:\n            res = min((n - 1) // 2 - odd + even, (n + 1) // 2 - even + odd)\n            for i in range(n):\n                if s[i] == \'0\':\n                    odd, even = even, odd\n                else:\n                    odd, even = even - 1, odd + 1\n                temp = min((n - 1) // 2 - odd + even, (n + 1) // 2 - even + odd)\n                res = min(res, temp)\n        return res\n```
['Python3']