C++ Solution | A Different Approach | Time - O(N) Space - O(1)
The idea is to calculate the minimum operations to convert to an alternate string for each of rotation (Type - 1 operation).\n\nSuppose the present string is s1
The idea is to calculate the minimum operations to convert to an alternate string for each of rotation (Type - 1 operation).\n\nSuppose the present string is `s1 = 1110010`\nand we know the operation to convert it to\n1. Alt starting with 1 (One Alt  = `1010101`) is, `one = 4`\n2. Alt starting with 0 (Zero Alt = `0101010`) is, `zero = 3`\n\nNow on the next move, we convert it to -> `s2 = 110010` + `1`\nTo calculate the `one` for this string, note that the first part of this string was already converted to one alt string while calculating `zero` for the previous string. \n\nSo, `one(s2) = zero(s1) + some additional cost`\n\nNow, to calculate this additional cost \n* we need to **remove** 1 if the popped character (1st at `s1`) was contributing to the zero calculation of `s1`, meaning if it was `1` or `not 0` \n* we need to **add** 1 if the added character does not match with the expected end character. This is dependent on whether the string is even or odd length. For Odd length string, One-Alt ends with 1 but Zero-Alt ends with 0.\n\nif `char` is the present character which is popped and added to back,\n\n`one(s2) = zero(s1) - (char != \'0\') + (char != expected end char of length n)`\nSimilarly,\n`zero(s2) = one(s1) - (char != \'1\') + (char != expected end char of length n)`\n\nWe can see that that for even length strings these 2 cancels each other.\n\nTo visualize this, we can make table which tracks the rotation and calculate the operation needed to transform to either of the alternating string (`1010101`  or `0101010`). \n\n\n![image](https://assets.leetcode.com/users/images/243ceb7e-6cb5-4401-bffe-8ec7d8085122_1623310385.5569072.png)\n\n\nThis shows how we can calculate the operation needed after one rotation. In practice, we won\'t be needing this table, rather keep running variables.\n\n\n```\nint minFlips(string s) {\n        \n        ios::sync_with_stdio(false);\n        cin.tie(NULL);\n        \n        int one = 0, zero = 0;\n        int n = s.length();\n        // one => operation needed to convert to 101010101... \n        // zero => operation needed to convert to 01010101...\n        // Calculate the operation needed from current position\n        for(int i = 0 ; i < n; i++)\n        {\n            if(i % 2 == 0 && s[i] == \'0\') one++;\n            if(i % 2 == 1 && s[i] == \'1\') one++;\n            \n            if(i % 2 == 0 && s[i] == \'1\') zero++;\n            if(i % 2 == 1 && s[i] == \'0\') zero++;\n        }\n        \n        int ans = min(one, zero);\n        int even = (n % 2) == 0;\n        \n        // This part is slightly tricky\n        // Explanation above\n        for(int i = 1; i < n; i++)\n        {\n            int new_one = even ? zero : zero - (s[i-1] != \'0\') + (s[i-1] != \'1\');\n            int new_zero = even ? one : one  - (s[i-1] != \'1\') + (s[i-1] != \'0\');\n            one = new_one;\n            zero = new_zero;\n            ans = min(ans,min(one, zero));\n            if(ans == 0) break;\n        }\n        return ans;\n    }\n```\t\n\t\n\t
[]