Ctrl K

Striped Runs on a Bracelet

Medium
A bracelet is a ring of beads in two colours: beads[i] is 0 or 1. Bead n - 1 sits next to bead 0. Call a stretch of length consecutive beads (going round the ring) striped if every two neighbouring beads in it have different colours. Count the striped stretches of length length. Stretches that start at different beads count as different.
Example 1
beads=[1, 0, 1, 1, 0, 1], length=3 → 2

Only the stretches starting at positions 0 and 3 (both 1, 0, 1) alternate; every other stretch of three has two equal neighbours.

Example 2
beads=[0, 1, 0, 1], length=4 → 4

The beads alternate all the way round, including across the seam between the last and first bead, so a stretch from each of the 4 positions works.

Example 3
beads=[1, 1, 1], length=3 → 0
Constraints
  • 3 ≤ n = len(beads) ≤ 10⁵
  • beads[i] is 0 or 1
  • 3 ≤ length ≤ n
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsShortest Stretch to the Goal MediumEvenly Stepped Stretches MediumLongest Run of Ones With One Fix MediumNearest k Values MediumWindows With a Small Product Medium
loading Python…
Input
beads=[1, 0, 1, 1, 0, 1], length=3
Expected
2

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