O(N) counting + sliding window (example & explanation)
IntuitionAlternating string can only be

101010.. pattern:
s[i] == 1 for all i % 2 == 0 and s[i] == 0 for all i%2 == 1


010101.. pattern
s[i] == 0 for all i %
# Intuition 

Alternating string can only be
1. 101010.. pattern: 
    s[i] == 1 for all i % 2 == 0 and s[i] == 0 for all i%2 == 1 
2. 010101.. pattern 
    s[i] == 0 for all i % 2 == 0 and s[i] == 1 for all i%2 == 1

1. #odd index slot =  N/2
2. #even index slot = N - #odd index slot.
XXX --> odd index = 1, even index = 0 2 
012

3. let cnt1 = sum(s[i]==1) for i %2 == 0
4. let cnt2 = sum(s[i]==1) for i %2 == 1

if we cannot use type-1 operation, the solution will be

5. #Swap for pattern1:  
(#even_slot - cnt1)      + cnt2.
#swap to change 0 to 1   + #swap to change 1 to 0

6. #Swap for pattern2:  
cnt1                     + (odd_slot - cnt2).
#swap to change 1 to 0   + #swap to change 0 to 1

ret = min(pattern1, pattern2).


if we can use type1, then we need to use sliding window.

if N is even:
// ABCD|ABCD  (N%2 == 0--> repeats)
// ----    even_n1 = A + C / odd_n1 = B + D  
//  -----  even_n1 = B + D / odd_n1 = A + C
you will find out the sliding window repeat.

if N is odd:

// ABCDE|ABCDE 
(N%2 == 1 --> even_n1' = odd_n1 + current.front, odd_n1' = even_n1'-current.front)

// -----     even_n1 = **A** + *C + E* / odd_n1 = **B + D**
//  ------   even_n1 = **B** + *D + A* / odd_n1 = **C + E**
//   ------  even_n1 = C + E + B / odd_n1 = D + A   


## try this testcase!
s = 11010
w/o tp1: 01010 (swap=1)
with tp1:10101 (shift=1, swap=0)

# Complexity
- Time complexity:
O(N)
- Space complexity:
O(1)

# Code
```cpp []
class Solution {
public:
    int minFlips(string s) {
        const int N = s.size();
        
        const int odd_slot = N / 2;
        const int even_slot = N - odd_slot;

        int even_n1 = 0;
        int odd_n1 = 0;
        for (int i = 0; i < N; i+=2) {
            even_n1 += s[i] - '0';
        }
        for (int i = 1; i < N; i+=2) {
            odd_n1 += s[i] - '0';
        }
        //even = 1, odd = 0
        int pattern1 = even_slot - even_n1 + odd_n1;
        // even = 0, odd = 1
        int pattern2 = even_n1 + odd_slot - odd_n1;
        int ret = std::min(pattern1, pattern2);

        // ABCDE|ABCDE (N%2 == 1 --> even_n1' = odd_n1 + current.front, odd_n1' = even_n1'-current.front)
        // -----     even_n1 = A + C + E / odd_n1 = B + D
        //  ------   even_n1 = B + D + A / odd_n1 = C + E
        //   ------  even_n1 = C + E + B / odd_n1 = D + A   

        // ABCD|ABCD  (N%2 == 0)
        // ----    even_n1 = A + C / odd_n1 = B + D  
        //  -----  even_n1 = B + D / odd_n1 = A + C

        // sliding window
        for (int i = 0; i < N; ++i) {
            const int idx = i; // remove 
            int even_new = odd_n1;
            int odd_new = even_n1;
            even_n1 = even_new;
            odd_n1 =  odd_new;
            if (N%2 == 1) {
                even_n1 += s[idx] - '0';
                odd_n1 -= s[idx] - '0';
            }
            pattern1 = even_slot - even_n1 + odd_n1;
            pattern2 = even_n1 + odd_slot - odd_n1;
            ret = std::min(ret, pattern1);
            ret = std::min(ret, pattern2);
        }
        return ret;
    }




};


```
['C++']