aa r X i v : . [ m a t h . C O ] S e p Some Notes on Pairs in Binary Strings
Jeremy M. Dover
Abstract
Seth [3] posed a problem that is equivalent to the following: howmany binary strings of length n have exactly k pairs of consecutive0s and exactly m pairs of consecutive 1s, where the first and lastbits are considered as being consecutive? In this paper, we providea closed form solution which also solves a related problem with someinteresting connections to other combinatorial sequences. Seth [3] posed the following problem: consider a microstate consisting of 8spins, where a microstate is a linear ordering of spins, each of which may bein the up or down state. Seth asks how many of these 8-spin microstateshave exactly 2 “up parallel pairs” and 2 “down parallel pairs”, where an“up parallel pair” is two consecutive up states, and the obvious meaningof consecutive for the linear ordering is extended so that the first and laststates are also considered consecutive. A “down parallel pair” is definedanalogously. Note that despite the first and last states being consideredconsecutive, the microstate is still considered to have a first and last state,so rotations of the state pattern are counted as being different.It is not hard to cast this problem into a question about binary strings,where an “up spin” is a 0, and a “down spin” a 1, namely finding the car-dinality of the set S ◦ ( n, k, m ), the set of all binary strings of length n with k pairs of consecutive 0s and m pairs of consecutive 1s, with the first andlast bits considered consecutive. In order to address this problem, we definethe related set Z ( n, k, m ) to be the number of binary sequences of length n that start with 0 and have k pairs of consecutive zeroes, and m pairs ofconsecutive ones, where the first and last bits are not considered consecutive.We denote the cardinality of Z ( n, k, m ) as z ( n, k, m ). We now show that | S ◦ ( n, k, m ) | can be determined in terms of the values z ( n, k, m ). In whatfollows, we will assume unless otherwise stated that the first and last bits ofa binary string are not considered consecutive. For brevity, we refer to a pairof consecutive 0s (resp. 1s) in a binary string as a 0-pair (resp. 1-pair); thisterminology will specifically not be used for the first and last bits, in thosecases where they are considered consecutive.1he first issue to address is that some of the binary strings in S ◦ ( n, k, m )begin with 1. However, we note that the operation of inverting each bit ofa binary string of length n is obviously a bijective involution from the setof all binary strings of length n onto itself, and shows that the number ofbinary strings that start with a 1 and have k m z ( n, m, k ).We know that the elements of Z ( n, k, m ) begin with 0, but we do notnecessarily know how they end, which is an important consideration whenanalyzing S ◦ ( n, k, m ). The following lemma provides an answer to this ques-tion. Lemma 1.1.
Let n, k, m be integers such that n ≥ and k, m ≥ , and let b be a binary string of length n containing k m b is the same as the first bit of b if and only if n + k + m is odd.Proof. Given a binary string b of length n , assign to each pair of consecutivebits (of which there are n −
1) the letter S if they are the same, and D if theyare different. Since b has k m m + k Ss, and thus there are n − − m − k Ds. Reading from left to right, we onlychange values in the string when we encounter a D, so it is easy to see thatthe last bit of the string depends only on the parity of n − − m − k . Ifthis value is odd, then the last bit of b is different from the first bit, whilethese bits will be the same if the number of Ds is even. Since n + m + k and n − − m − k have opposite parity, we obtain the result.Let’s use these facts to determine | S ◦ ( n, k, m ) | . Let b ∈ S ◦ ( n, k, m ) be abinary string. If the first and last bits of b are different, then by Lemma 1.1we must have n + k + m even, since b has k m b are the same, then b has either k − m k m − n + k + m − n + k + m is even. This shows that if n + k + m is odd, then there are no binary strings in S ◦ ( n, k, m ). But if n + k + m iseven, then all of the binary strings in Z ( n, k, m ) and Z ( n, k − , m ) are in S ◦ ( n, k, m ), as are the inversed of the string in Z ( n, m, k ) and Z ( n, m − , k ).Thus we have: S ◦ ( n, k, m ) = k + m odd z ( n, k, m ) + z ( n, k − , m )+ z ( n, m, k ) + z ( n, m − , k ) n + k + m even2he important takeaway from this section is that our original problemcan be solved strictly by consideration of the numbers z ( n, k, m ), which wefocus on exclusively in what follows. z ( n, k, m ) The boundary conditions for z ( n, k, m ) are fairly straightforward; for conve-nience we define z ( n, k, m ) = 0 for all n < k < m <
0. Noting that abinary string of length n only has n − z ( n, k, m ) = 0 for all integers k, m such that k + m ≥ n . Moreover, if k + m = n −
1, our numbers z ( n, k, m ) count the number of strings where allpairs of consecutive bits are identical, with the first bit of the string being 0.This forces the string to be entirely 0s, showing that for integers k, m with k + m = n − z ( n, k, m ) = 0 unless k = n − m = 0, in which case z ( n, n − ,
0) = 1.We now derive several different recurrence relations for the z ( n, k, m ),which each have different uses. Theorem 2.1.
Let n, k, m be integers such that n ≥ and k, m ≥ . Then z ( n, k, m ) = z ( n − , k − , m ) + z ( n − , k, m ) + z ( n − , m − , k ) .Proof. To prove this result, we count the size of Z ( n, k, m ) in two ways, oneof which is z ( n, k, m ) by definition. For the other count, let b ∈ Z ( n, k, m )and consider three cases:1. If b starts with 00, then b consists of a 0 followed by a string of n − k − m z ( n − , k − , m ).2. If b starts with 010, then b consists of 01 followed by a string of n − k m z ( n − , k, m ).3. If b starts with 011, then b consists of 01 followed by a string of n − k m − z ( n − , m − , k ).Note that if m = 0, no strings in Z ( n, k, m ) start with 011, but z ( n − , − , k )is defined to be 0, so this remains correct. Since these three cases count setsthat form a disjoint union of Z ( n, k, m ), we have the result.3ur next recurrence is somewhat more complicated and is not univer-sally applicable, but it counts in a way which quickly reveals an importantcorollary. Theorem 2.2.
Let n, k, m be integers such that n ≥ and k + m < n − .Then z ( n, k, m ) = k +1 X f =1 z ( n − f, m, k + 1 − f ) Proof.
Again we proceed by counting the cardinality of Z ( n, k, m ) in twoways, the first yielding z ( n, k, m ). To count this set in another way, we notethat since k + m < n −
1, any b ∈ Z ( n, k, m ) must contain at least one 1.Let f be the position of the first 1 in b , so f may vary between 1 and n − b has index 0). Prior to the first 1, the string consistsentirely of 0s, creating f − n − f starting with1 that contains exactly k + 1 − f m z ( n − f, m, k + 1 − f ) such strings. Thus we can write z ( n, k, m ) = n − X f =1 z ( n − f, m, k + 1 − f )Noting that z ( n − f, m, k +1 − f ) = 0 for all f > min { n, k +1 } and that k +1 Let n, m be integers with n ≥ and ≤ m < n − . Then z ( n, , m ) = z ( n − , m, .Proof. Since m < n − 1, we can apply Theorem 2.2 to z ( n, , m ) to obtain z ( n, , m ) = P f =1 z ( n − f, m, − f ). Evaluating the single term of thesummation yields the result.Our final recurrence is nice because it shows how we can reduce theproblem of calculating z ( n, k, m ) to just those values with m = 0.4 heorem 2.4. Let n, k, m be integers such that n ≥ , ≤ k ≤ n − , < m ≤ n − − k . Then z ( n, k, m ) = m X f =1 (cid:18) k + ff (cid:19)(cid:18) m − f − (cid:19) z ( n − m − f, k + f, n + k + m odd m X f =1 (cid:18) k + f − f − (cid:19)(cid:18) m − f − (cid:19) z ( n − m − f, k + f − , m X f =1 (cid:18) k + ff (cid:19)(cid:18) m − f − (cid:19) z ( n − m − f, k + f, n + k + m even Proof. Define the mapping ψ on a string b ∈ Z ( n, k, m ) to be the stringobtained by deleting from b all substrings of consecutive 1s of length greaterthan 1; notice that this operation is well-defined, and that ψ ( b ) is unique. Let f be the number of substrings of 1s removed from b , so clearly f is between1 and m . The length of ψ ( b ) must be n − ( m + f ), since the removal of asubstring of g consecutive 1s removes only g − ψ ( b ) hasno 1-pairs, since all such strings are removed, and no 1-pair can be createdby our deletion process. Indeed, the removal of a string of consecutive 1screates an additional 0-pair, unless that substring is removed from the end ofthe original string (it cannot come from the beginning since the string startswith a zero, by definition of Z ( n, k, m )).So to summarize, for any b ∈ Z ( n, k, m ) ending in either 0 or 01, thereexists a unique integer 1 ≤ f ≤ m and a unique binary string ψ ( b ) ∈ Z ( n − m − f, k + f, 0) such that b can be obtained from ψ ( b ) by injecting a total of m + f 1s into f b ∈ Z ( n, k, m ) ending in 11, there exists a unique integer1 ≤ f ≤ m and a unique binary string ψ ( b ) ∈ Z ( n − m − f, k + f − , b can be obtained from ψ ( b ) by injecting a total of m + f 1s into f − ψ ( b ) such that each injectedsubstring contains at least two 1s.To count the number of strings in Z ( n, k, m ) that end in 0 or 01, we followthe recipe above. Given any 1 ≤ f ≤ m , let b ′ ∈ Z ( n − m − f, k + f, z ( n − m − f, k + f, 0) choices. We can pick any f b ′ ,which can be done in (cid:0) k + ff (cid:1) ways. To inject our substrings of ones, since weknow each substring must have at least two 1s, and the remaining m − f (cid:0) m − f − (cid:1) ways. Each of the binary strings constructedthis way is contained in Z ( n, k, m ), and all are distinct as discussed above.5ence Z ( n, k, m ) contains m X f =1 (cid:18) k + ff (cid:19)(cid:18) m − f − (cid:19) z ( n − m − f, k + f, Z ( n, k, m ) that end in 11, we proceedas before. Given any 1 ≤ f ≤ m , we pick b ′ ∈ Z ( n − m − f, k + f − , b ′ is 0 if n − m − f + k + f − n − m + k − n + k + m isodd. However, if n + k + m is even, every string in Z ( n − m − f, k + f − , z ( n − m − f, k + f − , 0) choices for b ′ . Then from the k + f − b ′ , we choose f − (cid:0) k + f − f − (cid:1) ways. Finally, we can distribute the m + f 1s we need to addbetween these f − b ′ , such that each injected stringhas at least two 1s, in (cid:0) m − f − (cid:1) ways, exactly as above. Therefore, if n + k + m is odd, Z ( n, k, m ) contains no strings ending in 11, while if n + k + m is even, Z ( n, k, m ) contains: m X f =1 (cid:18) k + f − f − (cid:19)(cid:18) m − f − (cid:19) z ( n − m − f, k + f − , m = 0 and Terquem’s problem Theorem 2.4 reduces our problem of computing z ( n, k, m ) to the values of z ( n, k, z ( n, k, m ), we searched the OEIS [1] for the case m = 0, which yielded an unexpected connection with the sequence A046854.This sequence, which we call T ( n, k ), represents a triangle of numbers with n ≥ 0, 0 ≤ k ≤ n − T ( n, k ) = (cid:18)(cid:4) n + k (cid:5) k (cid:19) Combinatorially, this sequence arises as a solution to Terquem’s problem [2],namely providing the number of length k , increasing sequences of integers6rom { . . . n } that alternate parity and start with an odd number, as wellas the number of length k , increasing sequences of integers from { . . . n + 1 } that alternate parity and start with an even number.We can verify this numerical relationship with a quick induction proof: Theorem 3.1. Let n, k be integers such that n > and ≤ k ≤ n − . Then z ( n, k, 0) = T ( n − , k ) = (cid:0) ⌊ n + k − ⌋ k (cid:1) .Proof. We proceed via strong induction. It is easy to calculate z ( n, k, 0) forsmall values of n via enumeration: n = 1 Z (1 , , 0) = { } , z (1 , , 0) = T (0 , 0) = 1 n = 2 Z (2 , , 0) = { } , z (2 , , 0) = 1; Z (2 , , 0) = { } , z (2 , , 0) = 1 n = 3 Z (3 , , 0) = { } , Z (3 , , 0) = { } , Z (3 , , 0) = { } , Z (3 , , 1) = { } By way of induction, assume z ( n, k, 0) = (cid:0) ⌊ n + k − ⌋ k (cid:1) for all n < N , 0 ≤ k ≤ n − 1, and consider z ( N, k, z ( N, k, 0) = z ( N − , k − , 0) + z ( N − , k, 0) + z ( N − , − , k )= z ( N − , k − , 0) + z ( N − , k, z ( N, k, 0) = (cid:0) ⌊ N + k − ⌋ k − (cid:1) + (cid:0) ⌊ N + k − ⌋ k (cid:1) .A simple application of Pascal’s identity yields the result.Interestingly, it is possible to generate a bijection between all of the binarystrings in Z ( n, k,