Regular expression that contains no aab-substring

4.5k Views Asked by At

I need to construct a regular expression that defines a language containing all words that contain no aab-substring and end in bb.

I'm not too sure if this is correct - (a + (aaa)*)* b* bb - I'm not sure on how to allow multiple a's that are more than two a's

1

There are 1 best solutions below

6
On BEST ANSWER

Since a word in the language must end with $bb$, any $a$ or string of $a$s in the word will eventually be followed by a $b$. This means that if the word contains $aa$, it will necessarily contain $aab$. Thus, we cannot allow the substring $aa$ at all. Clearly a word may start with any number of $b$s, so let’s try starting the regular expression with $b^*$. Then we can alternate non-empty strings of $b$s with single $a$s as often as we want: $b^*(ab^+)^*$.

Now we just need to make sure that we get $bb$ on the end. This is actually a little tricky: a word generated by $b^*(ab^+)^*$ can end in any number of $b$s, including none at all. One way to handle the problem is to separate the words of the language with no $a$s from those that contain at least one $a$. The first set is easy: it’s generated by $b^*bb$. The second is generated by $b^*(ab^+)^*ab^*bb$, where I’ve pulled the last $a$ out of the $(ab^+)^*$ repeating block so that I know where the last block of $b$s starts.

Now put it together:

$$b^*bb+b^*(ab^+)^*ab^*bb=\big(\lambda+b^*(ab^+)^*a\big)b^*bb$$

or, if you don’t want to stick to strict regular expressions,

$$\big(\lambda+b^*(abb^*)^*a\big)b^*bb\;.$$

(I use $\lambda$ for the empty word; you may be used to $\epsilon$ instead.)

Added: Henning Makholm points out in the comments below that there is another nice way to cover all of these cases, one that is very compact if you allow the $^+$ operator:

$$\big((a+\lambda)b\big)^+b=\big((a+\lambda)b\big)\big((a+\lambda)b\big)^*b$$