Regular Expressions & Languages

Answer & solution
Official answer: 15
Approach: Interpret the regex structurally before doing a length-by-length count.
1. Derivation. The regular expression has one initial a followed by any number of pairs of a’s, with arbitrary b runs between and around them. It therefore accepts exactly the strings with an odd positive number of a’s. For any positive length ℓ exactly half of the 2^ℓ binary strings have odd a-parity, by flipping one fixed position. Lengths1 through 4 contribute 1+2+4+8=15; ε has even parity and is excluded.
2. Explanation and checks. The one initial a plus any number of pairs makes the a-count odd; the b* runs admit arbitrary b’s before, between and after those a’s. For length 3 the allowable counts are one a or three a’s, giving C(3,1)+C(3,3)=4. For length 4 they give C(4,1)+C(4,3)=8. Lengths 1 and 2 contribute one and two. The empty string has no a and cannot match the mandatory initial a.