> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Minimum Number of Flips to Make the Binary

> Tested Python solution for LeetCode 1888 with 28 pytest cases. Generate a practice environment with lcpy.

LeetCode 1888, [Medium](/catalog/medium). Topics: [String](/catalog/topics/string), [Dynamic Programming](/catalog/topics/dynamic-programming), [Sliding Window](/catalog/topics/sliding-window). [View on LeetCode](https://leetcode.com/problems/minimum-number-of-flips-to-make-the-binary-string-alternating/description/).

Generate this problem as a practice environment: tested reference solution, 28 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1888   # by problem number
lcpy gen -s minimum_number_of_flips_to_make_the_binary_string_alternating   # by problem name
```

## Problem

You are given a binary string `s`. You are allowed to perform two types of operations on the string in any sequence:

* **Type-1: Remove** the character at the start of the string `s` and **append** it to the end of the string.
* **Type-2: Pick** any character in `s` and **flip** its value, i.e., if its value is `'0'` it becomes `'1'` and vice-versa.

Return *the **minimum** number of **type-2** operations you need to perform* *such that* `s` *becomes **alternating**.*

The string is called **alternating** if no two adjacent characters are equal.

* For example, the strings `"010"` and `"1010"` are alternating, while the string `"0100"` is not.

### Examples

```
Input: s = "111000"
Output: 2
Explanation: Use the first operation two times to make s = "100011".
Then, use the second operation on the third and sixth elements to make s = "101010".
```

```
Input: s = "010"
Output: 0
Explanation: The string is already alternating.
```

```
Input: s = "1110"
Output: 1
Explanation: Use the second operation on the second element to make s = "1010".
```

### Constraints

* `1 <= s.length <= 10^5`
* `s[i]` is either `'0'` or `'1'`.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_number_of_flips_to_make_the_binary_string_alternating/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_number_of_flips_to_make_the_binary_string_alternating/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def min_flips(self, s: str) -> int:
        n = len(s)
        doubled = s + s
        # Mismatches of the window starting at index 0 against "0101..." and "1010..."
        mismatch = sum(doubled[i] != "01"[i & 1] for i in range(n))
        best = min(mismatch, n - mismatch)
        # Slide the rotation start: drop the left char, pick up the one entering the window
        for start in range(1, n):
            left = start - 1
            mismatch -= doubled[left] != "01"[left & 1]
            right = start + n - 1
            mismatch += doubled[right] != "01"[right & 1]
            best = min(best, mismatch, n - mismatch)
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.