1888. Minimum Number of Flips to Make the Binary String Alternating
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

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

# Code
```python3 []
class Solution:
    def minFlips(self, s: str) -> int:
        n = len(s)
        s = s + s  # Duplicate the string to account for rotations
        flip1 = [0] * (2 * n + 1)
        flip2 = [0] * (2 * n + 1)

        # Calculate the number of flips for the two alternating patterns
        for i in range(1, 2 * n + 1):
            flip1[i] = flip1[i - 1] + (s[i - 1] != '0' if i % 2 == 1 else s[i - 1] != '1')
            flip2[i] = flip2[i - 1] + (s[i - 1] != '1' if i % 2 == 1 else s[i - 1] != '0')

        # Find the minimum flips for any valid window of length n
        min_flips = float('inf')
        for i in range(n, 2 * n + 1):
            min_flips = min(min_flips, min(flip1[i] - flip1[i - n], flip2[i] - flip2[i - n]))

        return min_flips

```
['Python3']