[Python3] circular window
\n\nclass Solution:\n    def minFlips(self, s: str) -> int:\n        s = deque(int(x) for x in s)\n        \n        def fn(s, val=0): \n            """Return m
\n```\nclass Solution:\n    def minFlips(self, s: str) -> int:\n        s = deque(int(x) for x in s)\n        \n        def fn(s, val=0): \n            """Return min type-2 op to make s alternating."""\n            ans = int(s[0] != val)\n            for i in range(1, len(s)): \n                val ^= 1\n                if val != s[i]: ans += 1\n            return ans\n        \n        x0, x1 = fn(s, 0), fn(s, 1)\n        ans = min(x0, x1)\n        for _ in range(len(s)):\n            if len(s)&1: x0, x1 = x1 + s[0] - (s[0]^1), x0 - s[0] + (s[0]^1)\n            else: x0, x1 = x1, x0\n            s.append(s.popleft())\n            ans = min(ans, x0, x1)\n        return ans \n```\n\nEdited on 6/6/2021\nA more concise circular window implementation per @votrubac\n```\nclass Solution:\n    def minFlips(self, s: str) -> int:\n        s = [int(x) for x in s]\n        ans = inf\n        x01 = x10 = 0 \n        for i in range(2*len(s)): \n            x01 += s[i%len(s)]^i&1\n            x10 += s[i%len(s)]^(i+1)&1\n            if i+1 >= len(s): \n                if i >= len(s):\n                    x01 -= s[i-len(s)]^(i-len(s))&1\n                    x10 -= s[i-len(s)]^(i-len(s)+1)&1\n                ans = min(ans, x01, x10)\n        return ans \n```
['Python3']