All Exams Test series for 1 year @ ₹349 only
Question

The number of strings of length 4 that are generated by the regular expression $(0|\epsilon)1^+2^* (3|\epsilon)$, where | is an alternation character, {+, *} are quantification characters, and $\epsilon$ is the null string, is :

The correct answer is
12

Regex Analysis for String Generation

The regular expression is $(0|ε)1+2* (3|ε)$. We need to find the number of strings of length exactly 4.

Breakdown of components:

  • $(0|ε)$: Matches '0' (length 1) or null string $ε$ (length 0).
  • $1+$: Matches one or more '1's. Length $L_{1^+} \ge 1$.
  • $2*$: Matches zero or more '2's. Length $L_{2^*} \ge 0$.
  • $(3|ε)$: Matches '3' (length 1) or null string $ε$ (length 0).

The total length must be 4: $L_{(0|\epsilon)} + L_{1^+} + L_{2^*} + L_{(3|\epsilon)} = 4$.

Enumerating String Possibilities

We analyze cases based on the choices for the first $(0|ε)$ and last $(3|ε)$ parts.

First Part Choice Length ($L_{p1}$) Last Part Choice Length ($L_{p4}$) Required Length ($L_{p2} + L_{p3}$) Valid ($L_{p2}, L_{p3}$) Pairs Example Strings (Length 4) Count
$0$ 1 $3$ 1 $1 + L_{p2} + L_{p3} + 1 = 4 \implies L_{p2} + L_{p3} = 2$ (1, 1), (2, 0) $0123$, $0113$ 2
$0$ 1 $ε$ 0 $1 + L_{p2} + L_{p3} + 0 = 4 \implies L_{p2} + L_{p3} = 3$ (1, 2), (2, 1), (3, 0) $0122$, $0112$, $0111$ 3
$ε$ 0 $3$ 1 $0 + L_{p2} + L_{p3} + 1 = 4 \implies L_{p2} + L_{p3} = 3$ (1, 2), (2, 1), (3, 0) $1223$, $1123$, $1113$ 3
$ε$ 0 $ε$ 0 $0 + L_{p2} + L_{p3} + 0 = 4 \implies L_{p2} + L_{p3} = 4$ (1, 3), (2, 2), (3, 1), (4, 0) $1222$, $1122$, $1112$, $1111$ 4

Total String Count

Summing the counts from all cases:

Total Strings = 2 + 3 + 3 + 4 = 12.

Thus, there are 12 unique strings of length 4 generated by the regex.

Was this answer helpful?

Important Questions from Regular Expression - Teaching

  1. If $r_1$ and $r_2$ are regular expressions, then which of the following are correct. 

    A. $L(r_1 + r_2) = L(r_1) \cup L(r_2)$ 

    B. $L(r_1 \cdot r_2) = L(r_1) L(r_2)$ 

    C. $L((r_1)) = L(r_1)$ 

    D. $L(r_1^*) = (L(r_1))^*$

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App