Regular expression of a languageof two letters that must alternate?

122 Views Asked by At

Let be a language with the following alphabet : $\Sigma=\{a,b\}$

{ $w$ | $w$ does not contains $aa$ nor $bb$}

I know it can be graphed this way :

enter image description here

Can I write it as the following regular expression ?

$$(ab)^*+(ba)^*$$

1

There are 1 best solutions below

3
On BEST ANSWER

No, you're missing $aba$ and $bab$ etc.

Obviously, we're looking for words of one of the forms

  • $ababa…ba$
  • $ababa…b$
  • $baba…ba$
  • $baba…b$

That translates to the RE

$(ab)^*a + (ab)^* + b(ab)^* + b(ab)^*a$

In CS, this is easier written as $a?(ba)^*b?$, when the $?$ modifier is available.