C++ O(n) with explanation
case 1: there are 2n(even) elements in string s\nremoving the 1st char in s, and appending it to the end of s, that is same as connnecting the first element and
case 1: there are 2*n(even) elements in string s\nremoving the 1st char in s, and appending it to the end of s, that is same as connnecting the first element and the last element of s. AND string s become a circle. In this case, after all the operations are done, the adjacent element must be like "010101" or "10101010". \n![image](https://assets.leetcode.com/users/images/5c6e288b-7df5-487a-93c6-11f5922f3edc_1623201871.4536755.png)\nSo we just need to count the number of the elements in string s that differ from the results(010101... or 10101010...). And return the smaller number as the result.\ncase 2: there are 2*n + 1(odd) elements in string s. this looks more complicated, but it is actually same as the first case. what we need to do is to add one more element into the circle.\n![image](https://assets.leetcode.com/users/images/ab0e2d1b-c6f0-42fe-9e92-0426a531708c_1623202707.4024065.png)\ncalculate the number of operations(flip) for each position, and return the smallest one.\n```\nint minFlips(string s) {\n\t\tint len = s.size();\n\t\tint ret = 0;\n\t\tint res = 0;\n\t\tif (len % 2 == 0) {\n\t\t\tfor (int i = 0; i < len; i++) {\n\t\t\t\tif (s[i] != char((i % 2)+48)) { res++; }\n\t\t\t}\n\t\t\tfor (int i = 0; i < len; i++) {\n\t\t\t\tif (s[i] != char(((i + 1) % 2)+48) ) { ret++; }\n\t\t\t}\n\t\t\tret = ret < res ? ret : res;\n\t\t}\n\t\telse {\n\t\t\tres = INT32_MAX;\n\t\t\tvector<int> sum(len);\n\t\t\tvector<int> sum1(len);\n\t\t\t// begin with 0\n\t\t\tint pin1 = 0;\n\t\t\t// begin with 1\n\t\t\tint pin2 = 0;\n\t\t\tfor (int i = 0; i < len; i++) {\n\t\t\t\tif (s[i] != char((i % 2) + 48)) {\n\t\t\t\t\tpin1++;\n\t\t\t\t}\n\t\t\t\telse { \n\t\t\t\t\tpin2++;\n\t\t\t\t}\n\t\t\t\tsum[i] = pin1;\n\t\t\t\tsum1[i] = pin2;\n\t\t\t}\n\t\t\tfor (int i = 0; i < len; i++) {\n\t\t\t\tint tmp1 = (i!=0)? sum1[len - 1] - sum1[i] + sum[i - 1] : sum1[len - 1] - sum1[i];\n\t\t\t\tint tmp2 = (i!=0)? sum[len - 1] - sum[i] + sum1[i - 1] : sum[len - 1] - sum[i];\n\t\t\t\tres = min(min(tmp1, tmp2),res);\n\t\t\t}\n\t\t\treturn res;\n\t\t}\n\t\treturn ret;\n\t}\n```
[]