GATE / CS ARCHIVE

GATE CSE 2024 SET 2 / QUESTION 62

Count short strings with odd a-parity

Theory of Computation · Regular Expressions & Languages · Full GATE CSE 2024 Set 2 paper

GATE 2024 Set 2, Q62 2 marks · NAT
Report issue

Regular Expressions & Languages

Question 62, GATE 2024: L1 is described by b*ab*(ab*ab*)*. L2 contains all strings over {a,b} of length at most four. How many strings belong to both?
Tap to enlarge
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.

Original paper, page 39 · Answer key