Regular Expression question Σ = {a, b}, what's the expression containing either ba or abb?

85 Views Asked by At

So Σ = {a, b}, I need to find a regular expression representing all strings that contain either ba or abb. Here's what I got, but I wonder if it's correct.

(ab)(baUabb)(ab)(baUabb)(ab)*

It seems to be that it's a bit too long.

Would appreciate any help!

1

There are 1 best solutions below

0
On

Note that the patterns abb and ba cover almost all possibilities for what can happen when you switch from one letter to the other. The only case that's not covered are strings like aaaaaaab, specifically with only a single b at the end. With that in mind, I was thinking

(a+b.+|b+a.*)

Which looks for either of the two following patterns:

  • At least one a, then a b, then at least one more letter
  • At least one b, then an a, then the rest of the string