Javascript Solution
Intuition - Approach\nIt seems a lot more complicated in the beginning, than what it actually is.\nYou just have to realize that for any string there are only 2
# Intuition - Approach\nIt seems a lot more complicated in the beginning, than what it actually is.\nYou just have to realize that for any string there are only 2 scenarios you have to compare with, only 2 binary strings to compare with. \nOne that starts with a zero and alternating bits are folowing, and another that starts with 1 and alternating bits are folowing. Both have to be of same length as the input string.\n\n**The Type-2 operation**:\nWe are talking for a binary input, let\'s consider this one `10111010` (this is of length 8).\nSo, we need to have an alternating string of zeros and ones. As we already said, this means that our string will ether start with 0 or 1 and the rest of the bits will alternate.\nSo we have only two strings to compare our input with.\n\nWe will compare every bit of our input with the corresponding bit of `01010101`(this is **even** cause it starts with 0) and `10101010` (this is **odd** cause it starts with 1).\nThese, are both of length 8 of course, like our input.\n\nWe can construct our **even** and **odd** strings like this:\n``` javascript\nlet evenOfSameLength = "";\nlet oddOfSameLength = "";\n\n//\'^\' represents the XOR operation\nfor (let i = 0; i < s.length; i++) {\n    evenOfSameLength += i % 2; // "01..."\n    oddOfSameLength += i % 2 ^ 1; // "10..."\n}\n```\nSo, according to the type-2 operation, we have to count how many flips we need to make to our input so to be identical to our **even** or **odd** alternating strings. We check bit by bit and keep track of these flips and return the result. \nSounds easier now, but there is one more thing to bear in mind, the type-1 operation...\n\n**The Type-1 operation**:\nWe might need less flips for a given string if we move it\'s first bit to the end and compare every bit of our new string with our **even** and **odd** strings.\n\nLet\'s consider an input `1101`. Moving it\'s first bit to the end will be like `1011`(1st time we used the type-1 op), let\'s repeat that procedure `0111`(2), `1110` (3), `1101` (4). Ok, let\'s stop right here. \nSo, after running our first operation for sting.length times, we ended up in the same string. \nThere is no reason to continue using this procedure any more.\n\nIf we just double our string (so `1101` will become `11011101`), and compare an input.length window (so our first window would be `[1101]1101`) with our **even** and **odd** alternating strings, then we would have "mocked" the type-1 operation.\nAnd this is where the Sliding Window approach comes handy.\n\n# Finally:\n- We have to double our string and to compare each bit inside of a window of constant length (length of input) with our **even** and **odd** strings of same length. \n- Our window will slide towards the end of our doubled string until we reach the last bit. \n- We keep track of the needed flips for every window, and return the min number of flips.\n\n\n---\n\n\n\n# Enhancement:\nThere is no need to use the Type-1 operation on inputs of **even length** (if the length of the binary number is even, not the binary number itself).\n\nWe could do something like this:\n`if (s.length % 2 === 1) s += s;`\nWe double the s only if it\'s of odd length.\n\n**More about this**:\nLet\'s consider an **even length** alternating binary num `101010`. \nNow let\'s apply the type-1 operation `010101` (1st time), `101010` (2)...**its the same already**...\nEvery time it\'s the same as one of our even and odd alternating strings.\n> For even-length alternating bin number we just have **2 alternatives**, one that starts with `0` (even binary) and one that starts with `1` (odd binary).\n\nFor an **odd length** alternating binary num `1010101`, apply type-1 op `0101011` (1st time), `1010110` (2), `0101101`(3), `1011010`(4), `0110101`(5), `1101010`(6), `1010101`(7) ...**now it\'s the same**! \nWe had to apply the procedure string.length times to get the same binary number. \nThis would be the case for both an odd-length **even-binary** alternating number like `0101010`(starting with zero), and also for an odd-length **odd-binary** alternating number like `1010101` (starting with 1)\n> For odd-length bin number we ended up with **7 alternatives** (string.length alternatives)! \n\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n``` javascript []\nvar minFlips = (s) => {\n    if (!s || s.length < 1) return 0;\n\n    const initialLen = s.length;\n    if (initialLen % 2 === 1) s += s;\n\n    let evenCmp = 0;\n    let oddCmp = 0;\n    let minFlips = Infinity;\n    let end = 0;\n    let start = 0;\n\n    while (end < s.length) {\n        if (end % 2 !== Number(s[end])) evenCmp++;\n        if ((end % 2 ^ 1) !== Number(s[end])) oddCmp++;\n\n        if (end >= initialLen) {\n            if (start % 2 !== Number(s[start])) evenCmp--;\n            if ((start % 2 ^ 1) !== Number(s[start])) oddCmp--;\n            start++;\n        }\n\n        if (end >= initialLen - 1) {\n            minFlips = Math.min(evenCmp, oddCmp, minFlips);\n        }\n        end++;\n    }\n\n    return minFlips;\n};\n\n```\n\n\n\n# Some Explaination:\n\n``` javascript []\nif (end >= initialLen) {\n    if (start % 2 !== Number(s[start])) evenCmp--;\n    if ((start % 2 ^ 1) !== Number(s[start])) oddCmp--;\n    start++;\n}\n```\nFor input of length 4. If end >= 4, means we slided our window. \nSo we compare the **start** variable, and decrease our counters if we increased them before. \n`0[1100]110` So, the first time **start** is `0` and points to the first element of our string.\n\nWe don\'t consider those bits any more. We slided our window and want to know the flips needed for our current window not those old bits in the beginning.\n\n---\n\n\n\n``` javascript []\nif (end >= initialLen - 1) {\n    minFlips = Math.min(evenCmp, oddCmp, minFlips);\n}\n```\nFor input on length 4. If end>=3, it means we reached the 4th bit of our window `[0110]0110`, we save the minFlips for that window and lets go for the next bit. Of course the next bit will be the end of this window `0[1100]110` so after checking this bit (which is the last one of a window) we will also have to consider updating our **minFlips** variable.\n\n---\n\n\n``` javascript []\n(end % 2 ^ 1)\n```\n\'^\' represents the XOR operation\n\nThanks for reading this.\n\n
['Binary Search', 'Dynamic Programming', 'Greedy', 'Sliding Window', 'JavaScript']