Ctrl K

Zigzags on a Bead Ring

Easy
A string of beads is closed into a ring, and every bead is either white or black. The array beads lists their colours around the ring: 0 is white, 1 is black. The last bead touches the first one. Call three consecutive beads a **zigzag** when the middle one differs in colour from both of its neighbours (for example white–black–white). Return how many zigzags the ring contains. Triples that differ in at least one bead count separately.
Example 1
beads=[0, 1, 0, 1] → 4

Colours alternate all the way round, so each of the 4 beads is the middle of a pattern.

Example 2
beads=[1, 0, 1, 1, 0] → 3

The triples centred at indices 1 and 4 work (index 4 neighbours indices 3 and 0). The answer is 2.

Example 3
beads=[0, 0, 0] → 0

All beads share a colour, so there are no patterns.

Constraints
  • 3 ≤ len(beads) ≤ 100
  • beads[i] is 0 or 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRepeat Within Reach EasyValues One Apart EasyBest Window Average EasyStep Streak Score EasyNeighbour Sums Around a Circle Easy
loading Python…
Input
beads=[0, 1, 0, 1]
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 134 tests on the server, hidden ones included.