Prove that in a bit string, the string 01 occurs at most one more time than the string 10. Good morning. This is my first question in a StackExchange forum. Let me know if I'm doing anything incorrectly (posting in the wrong forum, etc.).
Prove that in a bit string, the string 01 occurs at most one more time than the string 10.
I wish to use structural induction for this proof.
Let Σ* be the set of all strings over the alphabet Σ. Let P(w) be the proposition "in the bit string w, the string 01 occurs at most one more time than the string 10".
Basis step: P(λ), where λ is the empty string, is true, because there are 0 strings 01 and 0 strings 10.
Recursive step: Here, I have to show that, if P(w) is true for some arbitrary w belonging to Σ*, then P(wx) is true, where x belongs to Σ. In other words, wx (the bit string w with one more bit x added to it) also has at most one 01 more than 10.
Suppose that P(w) is true. Then, in the bit string w, the string 01 occurs at most one more time than the string 10.
The bit string w can only end in 0 or in 1. When the bit x (which can be 0 or 1) is added, four possibilities occur:
1) w ends in 1 and x ends in 0. In this case, P(wx) is true, because wx has no extra 01's.
2) w ends in 0 and x ends in 0. In this case, P(wx) is also true.
3) w ends in 1 and x ends in 1. In this case, P(wx) is also true.
4) w ends in 0 and x ends in 1.
We have a problem in the last case, because there is one "01" added. By the inductive hypothesis, if w has at most one extra 01, then the proof would fail.
To complete the proof, I have to show that, if w ends in 0, it cannot have more 01's than 10's.
Here is an informal attempt. I will try to build w adding bits to it, and show that, if it ends with 0, for every 01 there will be a 10:
Suppose w is initially one or more 0's. If I add one or more 1's, then there would be one 01. If I add one 0, there will be one 10 to balance with that 01. If I keep adding 0's, the situation won't change. The only way to add one extra 01 would be to add one or more 1's. If I add one or more 0's to the end, one more 10 will be added and the number of 10's and 01's will be equal again.
By the above reasoning, if w ends in 0 and begins in 0, the number of 10's will always equal the number of 01's.
If w begins in one or more 1's, I can add 0 and apply the same reasoning as above, only that there will be one extra 10.
Is this reasoning consistent? How can I show this formally?
Thank you in advance.
 A: Lemma 1 $P(w11w) \iff P(w1w)$
Lemma 2 $P(w00w) \iff P(w0w)$
By iterating lemma 1 and 2 it suffices to prove this theorem only for the strings like 01010101, 10101, etc. (where no digit occurs twice in a row). The result is obviously true for these strings.
A: For $w ∈ Σ^*$, let $\mathbf{im}(w)$ be the number of occurrences of 01 is $w$ minus the number of occurrences of 10. ("im" is short for imbalance).
Since a string of length $k$ cannot occur as a substring of a string of length at most $k-1$, we have $\mathbf{im}(empty) = \mathbf{im}(0) = \mathbf{im}(1) = 0$.
Now let $w' ∈ Σ^*$ and $x, y ∈ Σ$ be given, let $w = w'x$, and consider what happens to $\mathbf{im}$ when you append $y$ to $w$:


*

*If $x = y$ then $\mathbf{im}(wy) = \mathbf{im}(w)$

*If $x = 0$ and $y = 1$, then $\mathbf{im}(wy) = \mathbf{im}(w) + 1$

*If $x = 1$ and $y = 0$, then $\mathbf{im}(wy) = \mathbf{im}(w) - 1$
Note however that in the case where $(x, y) = (0, 1)$, appending something to $wy$ can't increase the imbalance again: there's no z such that $w1z = wyz = w01$. Vice versa if $(x, y) = (1, 0)$: then you must increase before you can decrease again.
So we have the invariant that for all $v$: $-1 \leq \mathbf{im}(v) \leq 1$.
