[C++] O(N) Time O(1) Space by counting odd position / even positions '1's and '0's
A little bit explanation here. Maintain two vectors odd[2], and even[2]\nwhich respectively counts the 0s and 1s at the even and odd positions of the string. \n
A little bit explanation here. Maintain two vectors odd[2], and even[2]\nwhich respectively counts the 0s and 1s at the even and odd positions of the string. \nodd[0]->odd position 0s count\nodd[1]->odd position 1s count\n...\nTo make it alternativing, either we have all odd to be 1 && all even to be 0 || all odd to be 0 && all even to be 0. Therefore, if we **do not do any type-1 op**, the minimum number of ops is `min (odd[0] + even[1], odd[1] + even[0])`\n\nThen loop through s.size() cases where each time we move one starting char to the end (type-1 operation). To maintain and update the odd[] and even[] status, we can first delete the first char (assuming), and update the even[] result. Then, we swap odd[] and even[] (because after we shifting it by one). Lastly, we update odd[] or even[] by adding the last char. In the meantime, we update the minOps in each shift of starting char to the end. \n\n\n```\nclass Solution {\npublic:\n    int minFlips(string s) {\n        vector<int> odd(2, 0), even(2, 0); // 01 count ; \n        for (int i = 0; i < s.size(); i++){\n            if (i%2 == 0) even[s[i]-\'0\']++; \n            else odd[s[i] -\'0\']++; \n        }\n        \n        int minOps = min(odd[1] + even[0], odd[0]+even[1]); \n        for (int i = 0; i < s.size(); i++){\n\t\t\t// Assuming first char is deleted \n            char c = s[i];\n            even[c-\'0\']--; \n\t\t\t\n\t\t\t// Swap even[] and odd[]\n            auto tmp = even; \n            even = odd; \n            odd = tmp; \n\t\t\t\n            // Append last one \n            if ((s.size() - 1)%2 == 0) even[c-\'0\']++; \n            else odd[c-\'0\']++; \n\t\t\t\n\t\t\t// Update minOps global optimum\n            minOps = min(minOps, min(odd[1] + even[0], odd[0]+ even[1]));\n        }\n        \n        return minOps;\n    }\n};
[]