Regular Expression for the set of all binary strings that are of even length with at least 1 zero

396 Views Asked by At

Also followup question is the set of all binary strings that are of even length with at least two zeros.

But for both questions I'm thinking of building my regular expression with casework, no zeroes, 1 zero, and two zeroes.

For no zeros I have: $(11)^*$

For one zero there just seems to be too many edge cases to also keep the string of even length. You have to consider the zero being at the start or somewhere in the middle or at the end. All while having the expression match any number of odd 1s.

I'm confused how to do this and looking for any insights on how to do this problem and the follow up. Thanks

1

There are 1 best solutions below

5
On

Wlog, "10" or "01" or "00" needs to be somewhere, so we start with that.

There are two cases:

Case 1: (odd number of digits)(10|01|00)(odd number of digits)
Case 2: (even number of digits)(10|01|00)(even number of digits)

Case 1: (00|01|10|11)*(1|0)(10|01|00)(1|0)(00|01|10|11)*
Case 2: (00|01|10|11)*(10|01|00)(00|01|10|11)*

Edit: As Karl pointed out, these two cases end up actually being the same, so you really only need case 2.