> ## 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.

# Alternating Groups II Python Solution

> Tested Python solution for LeetCode 3208 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 3208, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Sliding Window](/catalog/topics/sliding-window). [View on LeetCode](https://leetcode.com/problems/alternating-groups-ii/description/).

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

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

## Problem

There is a circle of red and blue tiles. You are given an array of integers \<code>colors\</code> and an integer \<code>k\</code>. The color of tile \<code>i\</code> is represented by \<code>colors\[i]\</code>:

\<ul>
\<li>\<code>colors\[i] == 0\</code> means that tile \<code>i\</code> is \<strong>red\</strong>.\</li>
\<li>\<code>colors\[i] == 1\</code> means that tile \<code>i\</code> is \<strong>blue\</strong>.\</li>
\</ul>

\<p>An \<strong>alternating\</strong> group is every \<code>k\</code> contiguous tiles in the circle with \<strong>alternating\</strong> colors (each tile in the group except the first and last one has a different color from its \<strong>left\</strong> and \<strong>right\</strong> tiles).\</p>

\<p>Return the number of \<strong>alternating\</strong> groups.\</p>

\<p>\<strong>Note\</strong> that since \<code>colors\</code> represents a \<strong>circle\</strong>, the \<strong>first\</strong> and the \<strong>last\</strong> tiles are considered to be next to each other.\</p>

### Examples

![Example 1](https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-183519.png)

```
Input: colors = [0,1,0,1,0], k = 3
Output: 3
```

Alternating groups:

![Group 1](https://assets.leetcode.com/uploads/2024/05/28/screenshot-2024-05-28-182448.png)

![Group 2](https://assets.leetcode.com/uploads/2024/05/28/screenshot-2024-05-28-182844.png)

![Group 3](https://assets.leetcode.com/uploads/2024/05/28/screenshot-2024-05-28-183057.png)

![Example 2](https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-183907.png)

```
Input: colors = [0,1,0,0,1,0,1], k = 6
Output: 2
```

Alternating groups:

![Group 1](https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-184128.png)

![Group 2](https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-184240.png)

![Example 3](https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-184516.png)

```
Input: colors = [1,1,0,1], k = 4
Output: 0
```

### Constraints

* 3 \<= colors.length \<= 10^5
* 0 \<= colors\[i] \<= 1
* 3 \<= k \<= colors.length

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n + k)
    # Space: O(1)
    def number_of_alternating_groups(self, colors: list[int], k: int) -> int:
        n = len(colors)
        count = 0
        run = 1
        for i in range(1, n + k - 1):
            run = run + 1 if colors[i % n] != colors[(i - 1) % n] else 1
            if run >= k:
                count += 1
        return count
```

## Complexity

| Time | Space |
| - | - |
| O(n + k) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


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