7

I was discussing context free languages with some friends, when we came up with the following language over the alphabet $\Sigma=\{a,b\}$: $$L=\{xy\mid |x|=|y|\land \#_a(x)=\#_b(y)\}.$$

Intuitively, the language is defined by all words of even length having the same number of $a$s in the first half as the number of $b$s in the second half. We have realized that this is equivalent to $$\{xy\mid |\#_a(x)=\#_b(y)\land \#_b(x)=\#_a(y)\}.$$

The language seems not to be context free, since the very related language $L_2$ over the alphabet $\{a,b,c,d\}$ defined by $$L_2 = \big\{xy\mid x\in \{a,b\}^*, y\in\{c,d\}^* , |x|=|y|, \#_a(x) = \#_d(y) \big\}$$ is not (using the pumping lemma for CFL).

However, all the methods we have tried failed to show this. we were even able to show that the pumping lemma does not help with this language. We have also tried to instersect the language $L$ with some regular language hoping to get some well-known non-CFL language, however, we were not able to find such a language.

We have also tried to design a PDA or a context free grammar for this language, but we also failed to.

The question came from pure curiousity. I would appreciate any help or hint.

Narek Bojikian
  • 4,784
  • 1
  • 13
  • 36

1 Answers1

8

In fact, $$L=\{w:\#_a(w)=\#_b(w)\}.$$ On the one hand, $|x|=|y|=n/2$ and $\#_a(x)=\#_b(y)$ implies $$\#_a(xy)=\#_a(x)+\frac n2-\#_b(y)=\frac n2=\frac n2-\#_a(x)+\#_b(y)=\#_b(xy).$$ On the other hand, if $\#_a(w)=\#_b(w)$ (${}=n/2$), then $n=|w|$ is even, hence we can write $w=xy$ for $|x|=|y|=n/2$, and we have $$\#_a(x)=\#_a(w)-\#_a(y)=\frac n2-\#_a(y)=|y|-\#_a(y)=\#_b(y).$$

Thus, $L$ is context-free: it is recognized by a DPDA that keeps track on the stack of the balance between the number of $a$s and $b$s, and it is generated by the grammar $$S\to\epsilon\mid aSbS\mid bSaS.$$

Emil Jeřábek
  • 3,668
  • 17
  • 20