Simple O(n) time and O(1) space complexity
class Solution {\npublic:\n        \n        \n    int minFlips(string s) {\n            int n=s.length();\n            s+=s;\n            int k0=0,ki0=0,k1=1,k
class Solution {\npublic:\n        \n        \n    int minFlips(string s) {\n            int n=s.length();\n            s+=s;\n            int k0=0,ki0=0,k1=1,ki1=1,kf0,kf1;\n            int c1=0,c2=0,mn=INT_MAX;\n            for(int i=0;i<n;i++)\n                    {\n                            if(s[i]!=k0+\'0\')\n                                    c1++;\n                            if(s[i]!=k1+\'0\')\n                                    c2++;\n                                k0=k0^1;\n                                k1=k1^1;\n                                \n                    }\n            mn=min(c1,c2);\n            int lol=0;\n            for(int i=n;i<s.length();i++)\n                    {\n                        if((ki0+\'0\')!=s[lol])\n                                c1--;\n                        if((ki1+\'0\')!=s[lol])\n                                c2--;\n                        if(s[i]!=k0+\'0\')\n                                c1++;\n                        if(s[i]!=k1+\'0\')\n                                c2++;\n                                k0=k0^1;\n                                k1=k1^1;\n                    \n                        mn=min(mn,min(c1,c2));\n                    ki0=ki0^1;\n                    ki1=ki1^1;\n                    lol++;\n                                \n                    }\n\n        return mn ;\n    }\n};
[]