C++ solution ✔O(N) time and O(1) space
what is given to us are two operations\n operation 1: Remove the character at the start of the string s and append it to the end of the string.\n operation 2 : 
what is given to us are two operations\n* **operation 1:** Remove the character at the start of the string s and append it to the end of the string.\n* **operation 2 :** Pick any character in s and flip its value.\n\n**observation from operation 1 & 2:** Either we need to select a subarray that is starting from index 0, append it to the end of string OR flip the bit of any character.\n\n\n![image](https://assets.leetcode.com/users/images/522d842e-53af-45b6-8486-0ead3c0c946c_1680599960.6184368.png)\n\nas you can see in the image, what i am going to do is to select any subarray that is starting from 0 and ending on each index i (0 to n-1) and append to to the end of string.\n\nSo **total cost** = cost to make **string[0,i]** in alternating order + cost to make **string[i+1,n-1]** in alternating order.\nbut do you think it is enough \uD83E\uDD14?\n.\n.\n.\n.\n.\nNo, Let\'s find the reason.\nwhat if the length of the string [i+1,n-1] is **odd**\n\nLet\'s take one example.\n![image](https://assets.leetcode.com/users/images/aa7b0cf4-5d41-4f9e-81f8-9501442a2934_1680600821.4202433.png)\n\nIn that case if i append s1 at the end of s2 the final string will be **0 1 0 1 0 0 1 0** which is not in the alternating order.\n\nso what to do ??\n.\n.\n.\nwe can make a check if the s2 length is odd we need to convert s1 in  the different order.<br>\nwhat i mean by different order is, if s2 is in the order of **0101..** then s1 will be in the order of **1010...** and vice versa also and finally take the minimum value for the answer.\n\nHere is the code for it\n\n```\nclass Solution {\npublic:\n    int minFlips(string s) {\n        int n = s.size();\n        int ans = n;\n        int s01 = 0,s10 = 0;\n        for(int i = 0 ; i < n ; i++){\n            if(i%2 == (s[i]-\'0\')) s10++; \n            else s01++;\n        }\n        /**\n          * c01 and c10 is the cost to make string[0,i] in 0101 and 1010 order respectively.\n          **/\n        \n        /**\n          * s01 and s10 is the cost to make string[i+1,n-1] in 0101 and 1010 order respectively.\n          **/\n        \n        int c01 = 0,c10 = 0;\n        for(int i = 0 ; i < n ; i++){\n            if((n-i)%2 == 0) ans = min(ans,min(s10+c10,s01+c01));\n            else ans = min(ans,min(s10+c01,s01+c10));\n            if(s[i] == \'0\') s10--;\n            else s01--;\n            swap(s10,s01);\n            \n            if(i%2 == 0){\n                if(s[i] == \'0\') c10++;\n                else c01++;\n            }else{\n                if(s[i] == \'0\') c01++;\n                else c10++;\n            }\n        }\n        return ans;\n    }\n};\n```\n
['C']