Balanced Bitstring
Contest: Codeforces Round 668 (Div. 1)
Difficulty rating: 1500
Tags: implementation, strings

A bitstring is a string consisting only of the characters 0 and 1. A bitstring is called $$$k$$$-balanced if every substring of size $$$k$$$ of this bitstring has an equal amount of 0 and 1 characters ($$$\frac{k}{2}$$$ of each).

You are given an integer $$$k$$$ and a string $$$s$$$ which is composed only of characters 0, 1, and ?. You need to determine whether you can make a $$$k$$$-balanced bitstring by replacing every ? characters in $$$s$$$ with either 0 or 1.

A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

Input format:
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.

The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le k \le n \le 3 \cdot 10^5$$$, $$$k$$$ is even)  — the length of the string and the parameter for a balanced bitstring.

The next line contains the string $$$s$$$ ($$$|s| = n$$$). It is given that $$$s$$$ consists of only 0, 1, and ?.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.

Output format:
For each test case, print YES if we can replace every ? in $$$s$$$ with 0 or 1 such that the resulting bitstring is $$$k$$$-balanced, or NO if it is not possible.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
For the first test case, the string is already a $$$4$$$-balanced bitstring.

For the second test case, the string can be transformed into 101.

For the fourth test case, the string can be transformed into 0110.

For the fifth test case, the string can be transformed into 1100110.

Example input:
9
6 4
100110
3 2
1?1
3 2
1?0
4 4
????
7 4
1?0??1?
10 10
11??11??11
4 2
1??1
4 4
?0?0
6 2
????00

Example output:
YES
YES
NO
YES
YES
NO
NO
YES
NO

Editorial:
Let's denote the balanced bitstring (if any) deriving from $$$s$$$ to be $$$t$$$. Also, for the ease of the tutorial, let the strings be $$$0$$$-indexed (so the first character has index $$$0$$$ and the last character has index $$$n - 1$$$).
First of all, let's prove a very important observation: for every $$$i$$$ such that $$$0 \leq i < n - k$$$, $$$t_i = t_{i + k}$$$. This is because the length $$$k$$$ substrings starting at $$$i$$$ and $$$i + 1$$$ share the $$$k - 1$$$ characters $$$t_{i + 1} \dots t_{i + k - 1}$$$, so in order for both strings to have the same number of 1 characters, their remaining characters $$$t_i$$$ and $$$t_{i + k}$$$ must both be 1, or both be 0.
Extending this fact, we can easily prove that $$$t_i = t_j$$$ if $$$i \equiv j \pmod{k}$$$. So first of all, for each $$$0 \leq i < k$$$, we need to find out if all $$$s_j$$$ such that $$$j\text{ mod }k = i$$$ can be converted to the same character (i.e. there can't exist both 0 and 1 among these characters). Furthermore, we can deduce some information for $$$t_i$$$: it must be 0 if at least one character among $$$s_j$$$ is 0, must be 1 if at least one character among $$$s_j$$$ is 1, or it can be undecided and can be freely assigned to 0 or 1 if all $$$s_j$$$ are ?.
Lastly, we need to check if we can make the substring $$$t_0t_1 \dots t_{k - 1}$$$ have exactly half of the characters are equal to 1 (we don't need to check for any other substring, because the condition $$$t_i = t_{i + k}$$$ implies that all the substrings of size $$$k$$$ will have the same number of 1 characters). We simply need to check if the number of decided 1 characters and the number of decided 0 characters do not exceed $$$\frac{k}{2}$$$. It can easily be shown that if these numbers don't exceed this value then we can assign the undecided characters so that half of the characters are 1, and if one exceeds then it is impossible to do so.