Given two words $w_1$,$w_2$, is it decidable if the language $L$ of words containing equal number of $w_1$ and $w_2$ is regular?
First some definitions:
They could be made more concise, and the notations could be improved if they are to be used in proofs. This is only a first draft.
Given two words $w_1$ and $w_2$,
we say that:
$w_1$ always occurs with $w_2$, noted $w_1\triangleleft w_2$, iff
- for any string $s$ such that
$s=xw_2y$ with $\mid x\mid,\, \mid y\mid\ \geq \mid w_1\mid +\mid
w_2\mid$ and $|x|_0,|x|_1|,|y|_0,|y|_1| \geq 1$ there is another decomposition $s=x'w_1y'$.
Note: The condition that $x$ and $y$ each contain at least a 0 and a 1 is required by a pathological case (found by @sdcvvc): $w_1=1^i0$, $w_2=v1^{i+j}$ and $y\in1^*$, and its symetrical variants.
- there is a string $s=xw_2y$ with $\mid x\mid,\, \mid y\mid\ \geq \mid w_1\mid +\mid w_2\mid$ such that there is at most one decomposition $s=x'w_1y'$
$w_1$ always cooccurs with $w_2$, noted $w_1\triangleleft \triangleright\,w_2$, iff each always occur with the other,
$w_1$ and $w_2$ occur independently, noted $w_1\triangleright \triangleleft\,w_2$, iff neither one always occur with
the other,
$w_1$ always occurs $m$ times or more than $w_2$, noted $w_1\triangleleft_m w_2$, iff for any string $s$ such that
$s=xw_2y$ with $\mid x\mid,\ \mid y\mid|\ \geq \mid w_1\mid +\mid
w_2\mid$ there are $m$ other decompositions $s=x_iw_1y_i$ for
$i\in[1,m]$ such that $i\neq j$ implies $x_i\neq x_j$.
These definitions are constructed so that we can ignore what happens at
the ends of the string where $w_1$ and $w_2$ are supposed to occur.
Boundary effects at the end of the string have to be analyzed
separately, but they represent a finite number of cases (actually I think I forgot one or two such boundary sub-cases in my first analysis below, but it does not really matter).
The definitions are compatible with overlap of occurrences.
There are 4 main cases to consider (ignoring the symetry between $w_1$ and $w_2$):
$w_1\triangleleft
\triangleright\,w_2$
Both words come necessarily together, except possibly at the ends of the string. This concerns only pairs of the form $1^i0$ and $01^i$, or $0^i1$ and $10^i$. This is easily recognized by a finite automaton that only checks for lone occurences at both ends of the string to be recognized, to make sure there is a lone occurrence at both ends or at neither end. There is also the degenerate case when $w_1=w_2$: then the language L is obviously regular.
$w_1\triangleleft w_2$, but not $w_2\triangleleft w_1$
One of the 2 words cannot occur without the other, but the converse is not true (except possibly at the ends of the string). This happens when:
$w_1$ is a substring of $w_2$:then a finite automaton can just check that $w_1$ does not occur outside an instance of $w_2$.
$w_1=1^i0$ and $w_2=v1^j$ for some word $v\in\{0,1\}^*$, $v\neq01^i$: then a finite automaton check as in the previous case that $w_1$ does not occur separated from $w_2$. However, the automaton allows counting one extra instance of $w_1$ that will allow acceptance if $w_2$ is a suffix of the string. There are three other symetrical cases (1-0 symmetry and left-right symetry).
$w_1\triangleleft_2 w_2$
One of the 2 words occurs twice in the other. That can be recognized by an a finite automation that checks that the smaller word never occurs in the string. The is also a slightly more complex variant that combines the two variations of case 2. In this case the automaton checks that the smaller string $1^i0$ never occurs, except possibly as part of $v$ in the larger one $v1^j$ coming as a suffix of the string (and 3 other cases by symetry).
$w_1\triangleright \triangleleft\,w_2$
The 2 words can occur independently of each other. We build
a generalized-sequential-machine (gsm) $G$ that output $a$ when it recognizes an occurrence of $w_1$ and
$b$ when recognizing an occurrence of $w_2$, and forgets everything
else. The language $L$ is regular only if the language $G(L)$ is
regular. But $G(L)=\{w\in\{a,b\}^*\mid\ \mid w\mid_a=\mid w\mid_b\}$ which is clearly context-free and not regular. Hence $L$ is not regular.
Actually we have $L=G^{-1}(G(L))$. Since regular languages and context-free languages are closed under gsm mapping and inverse gsm mapping, we know also that $L$ is context free.
One way to organize a formal proof could be the following. First build
a PDA that recognizes the language. Actually it can be done with a
1-counter machine, but it is easier to have two stack symbols to avoid duplicating the finite control. Then, for the cases where it should be a FA, show
that the counter can be bounded by a constant that depends only on the
two words. For the other cases show that the counter can reach any
arbitrary value. Of course, the PDA should be organized so that the
proofs are easy enough to carry.
Representing the FA as a 2-stack-symbols PDA is probably the simplest representation for it. In the non-regular case, the finite control part of the PDA is the same as that of the GSM in the proof sketch above. Instead of outputting $a$'s and $b$'s like the GSM, the PDA counts the difference in number with the stack.