C++ Solution, sliding window. O(N) Time, O(1) Space
\n### Idea\n- For the 1st operation, we can simply do s += s to append the whole string to the end. \n- then we make two different string with the same length b
\n### Idea\n- For the 1st operation, we can simply do `s += s` to append the whole string to the end. \n- then we make two different string with the same length by `01` and `10` alternative. for example: s = `11100`\n    - `do s += s`:  s = `1110011100`\n    - `goal 1`:  s1= `1010101010`\n    - `goal 2`:  s2= `0101010101`\n- finally, use sliding window\uFF08size n\uFF09to compare s to both s1 and s2.\n- why we can double s to fullfil the first operation, let\'s look at the same example  s = `11100`:`\n\t- double s: `1110011100`\n\t- size n window: |11100|11100 ==>  1|11001|1100 ==> 11|10011|100 and so on, when we move one step of the sliding window, it is the same to append 1 more element from beginning.\n### Time complexity\n- Time O(N)\n- Space O(N) ==> O(1)\n\n### Solution\n\n- C++, O(N) space is easier to understand.\n```\nclass Solution {\npublic:\n    int minFlips(string s) {\n        int n = s.size();\n        s += s;\n        string s1, s2;\n        \n        for(int i = 0; i < s.size(); i++) {\n            s1 += i % 2 ? \'0\' : \'1\';\n            s2 += i % 2 ? \'1\' : \'0\';\n        }\n        int ans1 = 0, ans2 = 0, ans = INT_MAX;\n        for(int i = 0; i < s.size(); i++) {\n            if(s1[i] != s[i]) ++ans1;\n            if(s2[i] != s[i]) ++ans2;\n            if(i >= n) { //the most left element is outside of sliding window, we need to subtract the ans if we did `flip` before.\n                if(s1[i - n] != s[i - n]) --ans1;\n                if(s2[i - n] != s[i - n]) --ans2;\n            }\n            if(i >= n - 1)\n                ans = min({ans1, ans2, ans});\n        }\n        return ans;\n    }\n};\n```\n- base on above solution, we can come up with O(1) space solution.\n```\n\nclass Solution {\npublic:\n    int minFlips(string s) {\n        int n = s.size();\n        int ans1 = 0, ans2 = 0, ans = INT_MAX;\n        for(int i = 0; i < 2 * n; i++) {\n            if(i < n) s[i] -= \'0\'; //make \'1\' and \'0\' to be integer 1 and 0.\n            if(i % 2 != s[i % n]) ++ans1;\n            if((i + 1) % 2 != s[i % n]) ++ans2;\n            if(i >= n) {\n                if((i - n) % 2 != s[i - n]) --ans1;\n                if((i - n + 1) % 2 != s[i - n]) --ans2;\n            }\n            if(i >= n - 1)\n                ans = min({ans1, ans2, ans});\n        }\n        return ans;\n    }\n};\n```
[]