Language contex-free or not?

50 Views Asked by At

I was wondering whether this language is context-free or not?

It's $L = \{ AB ~|~ |A| = |B| \text{ and } A \neq B \}$ .

The alphabet is $\{ a, b \}$.

In my textbook it's written that it is Context-free, but I can't seem to find proper grammar for it.

How can I keep that the words are always different? If it's not CF, how can I prove it with the pumping lemma?

1

There are 1 best solutions below

0
On

The solution comes from looking at the problem with a bit of a squint. I'll just describe the trick (although you'll find it a lot more satisfying to figure it out yourself, so you might not want to read this yet.)

We know that $|A| = |B| \text{ and } A \ne B$. That means there is at least one $i$ for which $A_i \ne B_i$. Now consider the complete sentence $W = AB$, and let $n = |A|$. Clearly $A_i$ is $W_i$ and $B_i$ is $W_{n+i}$. That means that

$$W = \Sigma^{i-1}A_i\Sigma^{n-i}\Sigma^{i-1}B_i\Sigma^{n-i}$$

But that's the same as

$$W = \Sigma^{i-1}A_i\Sigma^{i-1}\Sigma^{n-i}B_i\Sigma^{n-i}$$

In other words, $W$ is the concatenation of two odd-length substrings whose middle characters differ.