Tap here to switch tabs
Problem
Submissions

Problem: Alternating Groups II

med
30 min
Try to solve the Alternating Groups II problem.

Statement

There is a circle of red and blue tiles represented by an integer array colors and an integer k. The color of tile i is denoted by colors[i], where colors[i] is equal to 00 for a red tile and 11 for a blue tile.

An alternating group is defined as a sequence of k contiguous tiles in the circle such that every adjacent pair of tiles in the sequence has alternating colors, meaning every tile has a different color from its adjacent neighbors within the sequence.

Return the total number of alternating groups of length k.

Note: Because colors represents a circle, the first tile and the last tile are adjacent.

Constraints:

  • 3≤3 \leq colors.length ≤105\leq 10^5

  • 0≤0 \leq colors[i] ≤1\leq 1

  • 3≤3 \leq k ≤\leq colors.length

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Alternating Groups II

med
30 min
Try to solve the Alternating Groups II problem.

Statement

There is a circle of red and blue tiles represented by an integer array colors and an integer k. The color of tile i is denoted by colors[i], where colors[i] is equal to 00 for a red tile and 11 for a blue tile.

An alternating group is defined as a sequence of k contiguous tiles in the circle such that every adjacent pair of tiles in the sequence has alternating colors, meaning every tile has a different color from its adjacent neighbors within the sequence.

Return the total number of alternating groups of length k.

Note: Because colors represents a circle, the first tile and the last tile are adjacent.

Constraints:

  • 3≤3 \leq colors.length ≤105\leq 10^5

  • 0≤0 \leq colors[i] ≤1\leq 1

  • 3≤3 \leq k ≤\leq colors.length