C++ | O(N) | 44ms
What basically done here is if for any i-1, i if s[i-1]==s[i], then we do something like rotation. I\'ll take an example,\n\nsuppose s = 10101010011010011100\n\
What basically done here is if for any i-1, i if s[i-1]==s[i], then we do something like rotation. I\'ll take an example,\n\nsuppose s = 10101010011010011100\n\nwe see that s can be written as 1010101001 (s1) + 1010011100 (s2)\nso if rotation is done than it will become s2+ s1 i.e., 1010011100 + 1010101001 = 101001110011010101001\n\nHere, \'+\' is the concatenation operator.\n\nwe did this to make the string have similar value at s[0] and s[s.length()-1].\n\nthis prevents the rotated \'s\' to have better answer in circular manner, if \'s\' has a good answer than we just need to scan \'s\' in linear fashion and not worry about it having a better answer with rotation.\n\nI think taking look at the code will clear the doubts.\n\n```\nclass Solution {\npublic:\n    int minFlips(string s) {\n        int n = s.length();\n        int cnt = 0;\n        int t = 1;\n        int pref[n][2];\n        for(int i=0;i<n;i++){\n            if(t){\n                if(s[i]==\'0\')\n                    cnt++;\n                t^=1;\n            }\n            else{\n                if(s[i]==\'1\')\n                    cnt++;\n                t^=1;\n            }\n            pref[i][0]=i+1-cnt;\n            pref[i][1]=cnt;\n        }\n        int suf[n][2];\n        suf[0][1]=cnt;\n        suf[0][0]=n-cnt;\n        for(int i=1;i<n;i++){\n            if(s[i-1]==\'1\'){\n                suf[i][0]=suf[i-1][1];\n                suf[i][1]=suf[i-1][0]-1;\n            }\n            else{\n                suf[i][1]=suf[i-1][0];\n                suf[i][0]=suf[i-1][1]-1;\n            }\n            \n        }\n        int ans =min(pref[n-1][0],pref[n-1][1]);\n        for(int i=1;i<n;i++){\n            if(s[i]==s[i-1]){\n                int len1 = i;\n                int len2 = n-i;\n                if((len2&1)){\n                    ans=min(ans,suf[i][1]+pref[i-1][0]);\n                    ans=min(ans,suf[i][0]+pref[i-1][1]);\n                }\n                else{\n                    ans=min(ans,suf[i][1]+pref[i-1][1]);\n                    ans=min(ans,suf[i][0]+pref[i-1][0]);\n                }\n            }\n            \n        }\n        return ans;\n        \n    }\n};\n```
[]