Minimum Flips to Make a Binary String Alternating After Any Rotation
IntuitionApproach1. Problem Understanding
We are given a binary string s, and we can perform the following operations:2. Observations3. Solution With Sliding Wi
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->
**1. Problem Understanding**
We are given a binary string s, and we can perform the following operations:

    - TYPE-1: Rotate the string by moving the first character to the end.
    - TYPE-2: Flip any character(change 0 → 1 or 1 → 0).

    **Our goal is to find the least number of different times**

**2. Observations**

    - An alternating binary string has the form: "0101010..."(begin with 0) || "10101010"(begin with 1)

    - Since Type-1 (rotation) allows any cyclic permutation, we need to check all possible rotations of s.

    - nstead of rotating s multiple times, we duplicate s to form newS = s + s and use Sliding Window to efficiently check all possible rotations in one pass.

**3. Solution With Sliding Window**
    
    Step_1: We will create 2 alternating strings starting from 0 and 1 with 2*n characters  And we will create newS = s+s

    Step_2: Iterate through the first window of length [0,n) and count the number of mismatches between newS and newBO,newBZ

    Step_3: We will using Sliding Window

       - Remove the leftmost character from the window and update flip counts.
       - Add the next character from newS and update flip counts.
       - Keep track of the minimum flips found.  
    
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ --> 

O (n)

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->  O (n)

# Code
```cpp []
// 1 110001 11000
// 0 101010 10101 -> 4
// 1 010101 01010 -> 2
class Solution {
public:
    int minFlips(string s) {
        ios_base::sync_with_stdio(false);
        cin.tie(nullptr);
        cout.tie(nullptr);
        int n = s.size();
        string newBZ = "",newBO = "";
        for(int i=0;i<2*n;++i){
            newBZ+=(i%2==0?"0":"1");
            newBO+=(i%2==0?"1":"0");
        }

        string newS = s+s;
        
        if(newS==newBZ||newS==newBO) return 0;

        int ans = INT_MAX,cnt1 = 0,cnt2 = 0;

        for(int i=0;i<n;++i){
            if(newS[i]!=newBZ[i]) cnt1++;
            if(newS[i]!=newBO[i]) cnt2++;
        }
        ans = min({ans,cnt1,cnt2});
        <!-- cout << ans << " ";  -->
        for(int i=n;i<2*n;++i){
            if(newS[i-n]!=newBZ[i-n]) cnt1--;
            if(newS[i-n]!=newBO[i-n]) cnt2--;

            if(newS[i]!=newBZ[i]) cnt1++;
            if(newS[i]!=newBO[i]) cnt2++;

            <!-- cout << ans << " "; -->
            ans = min({ans,cnt1,cnt2});
        }
        return ans;
    }
};

```
['Two Pointers', 'String', 'Dynamic Programming', 'Greedy', 'Sliding Window', 'C++']