Ctrl K

Flipping Windows in a Bit String

Medium
You are given a string bits made of the characters 0 and 1, and a number width. In one move you may pick any contiguous stretch of the string of length exactly width and invert it: every 0 in it becomes 1 and every 1 becomes 0. You may make any number of moves, including none. How many different strings can you end up with? The original string counts too. The answer can be large, so return it modulo 10⁹ + 7.
Example 1
bits='0110', width=2 → 8

There are three windows of length 2 (starting at 0, 1 and 2), and every subset of them yields its own string: 2³ = 8 variants, the original included.

Example 2
bits='101', width=3 → 2

There is a single window, the whole string, giving "101" and "010".

Example 3
bits='1', width=1 → 2
Constraints
  • 1 ≤ width ≤ bits.length ≤ 10⁵
  • bits consists only of the characters 0 and 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNumber in Roman Numerals MediumMultiplying Digit Strings MediumFraction as a Decimal MediumFour-Operation Calculator MediumEvery Bracketing Result Medium
loading Python…
Input
bits='0110', width=2
Expected
8

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