0

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!

J.-E. Pin
  • 40,163

1 Answers1

-1

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
Arthur
  • 199,419