[C++, O(N) time, O(1) space] Concise and easy to understand
\nclass Solution {\npublic:\n    int minFlips(string s) {\n        int n=s.size();\n        vector<vector<int> > cnt(2, vector<int>(2, 0));\n        \n        /
```\nclass Solution {\npublic:\n    int minFlips(string s) {\n        int n=s.size();\n        vector<vector<int> > cnt(2, vector<int>(2, 0));\n        \n        // cnt[0][0] = number of \'0\' in even positions\n        // cnt[0][1] = number of \'0\' in odd  positions\n        // cnt[1][0] = number of \'1\' in even positions\n        // cnt[1][1] = number of \'1\' in odd  positions\n        \n        for(int i=0; i<n; i++){\n            cnt[s[i]-\'0\'][i%2]++;\n        }\n        int ans=cnt[1][0]+cnt[0][1]; // cost to make all \'0\' in even positions and all \'1\' in odd positions\n        ans=min(ans, cnt[0][0]+cnt[1][1]); // cost to make all \'1\' in even positions and all \'0\' in odd positions\n        for(int i=0; i<n; i++){\n            cnt[s[i]-\'0\'][i%2]--;\n            cnt[s[i]-\'0\'][(n+i)%2]++; // sending this character at the end of the string\n            ans=min(ans, cnt[1][0]+cnt[0][1]);\n            ans=min(ans, cnt[0][0]+cnt[1][1]);\n        }\n        return ans;\n    }\n};\n```
[]