[100% Solution 4ms~12ms] Time O(N) + Space O(1) + Optimized Cpp Code
\nTime: [4ms~12ms] 100% of all C++ submission\n      O(N)\nSpace: O(1)\n\n\nThe solution is somehow like cyclic shift. It is a interesting journey to continuely
```\nTime: [4ms~12ms] 100% of all C++ submission\n      O(N)\nSpace: O(1)\n```\n\nThe solution is somehow like cyclic shift. It is a interesting journey to continuely optimize the code. It may take a while to understand the codes.\n\nFrom 0 to n, we count required operations in case the result begin with 0 and 1.\nIn case it require m operations if the result begin with 1, then n-m is require to make the result begin with 0. (remove one variant).\n\nDue to the first kind of operation, if n is odd, we scan from 0 to n-1 for the second time. This time we count required operations of a sliding window as [i+1,n] [0, i].\n\nIn the following code, we use some trick to shorten codes.\n```\n//Trick 1#\nfor(; i<n; ++i, b^=1) {\n\tm += (c[i]-\'0\'==b);\n}\n// equals to\nfor(; i<n; ++i, b^=1) {\n\tif(c[i]-\'0\'==b)\n\t\t++m;\n}\n```\n\n```\n//Trick 2#\nfor(i=0; i<n-1; ++i, b^=1) {\n\tm += 2*(c[i]-\'0\'==b)-1;\n\tans=min(ans, min(m, n-m));\n}\n// equals to\nfor(i=0; i<n-1; ++i, b^=1) {\n\tif(c[i]-\'0\'==b) {\n\t\t++m;\n\t} else {\n\t\t--m;\n\t}\n}\n```\nAnd the code submitted is as the following:\n```\nclass Solution {\npublic:\n    int minFlips(string s) {\n        const char * c=s.c_str();\n        int n=s.size(), m=0, ans, i=0, b=0;\n        for(; i<n; ++i, b^=1) {\n            m += (c[i]-\'0\'==b);\n        }\n        ans=min(m, n-m);\n        if(b) {\n            for(i=0; i<n-1; ++i, b^=1) {\n                m += 2*(c[i]-\'0\'==b)-1;\n                ans=min(ans, min(m, n-m));\n            }\n        }\n        return ans;\n    }\n};\n```
[]