The number of strings of length 4 that are generated by the regular expression…
2016
The number of strings of length 4 that are generated by the regular expression (0∣ϵ)1+2∗(3∣ϵ) where | is an alternation character and {+, *} are quantification characters, is :
- A.
08
- B.
10
- C.
11
- D.
12
Attempted by 100 students.
Show answer & explanation
Correct answer: D
Correct Answer is 12.
The expression is:
(0∣ϵ)1+2∗(3∣ϵ)
Let's break down the components:
Prefix (0∣ϵ): The string can start with a 0 (length 1) or nothing (ϵ, length 0).
Middle 1+: There must be at least one 1 (length ≥ 1).
Middle 2∗: There can be zero or more 2s (length ≥ 0).
Suffix (3∣ϵ): The string can end with a 3 (length 1) or nothing (ϵ, length 0).
To find strings of exactly length 4, we can split the problem into 4 cases based on the choices made for the Prefix and Suffix.
Step-by-Step Calculation
Case 1: Prefix is ϵ and Suffix is ϵ
Remaining Expression: 1+2∗
Target Length: We need the middle section to have a total length of 4.
Combinations: We must have at least one 1. The rest are 2s.
1111 (Four 1s, zero 2s)
1112 (Three 1s, one 2)
1122 (Two 1s, two 2s)
1222 (One 1, three 2s)
Count: 4 strings
Case 2: Prefix is 0 and Suffix is ϵ
Remaining Expression: 01+2∗
Target Length: The 0 takes up 1 character. We need the 1+2∗ part to have a length of 3.
Combinations:
0 + 111 → 0111
0 + 112 → 0112
0 + 122 → 0122
Count: 3 strings
Case 3: Prefix is ϵ and Suffix is 3
Remaining Expression: 1+2∗3
Target Length: The 3 takes up 1 character. We need the 1+2∗ part to have a length of 3.
Combinations:
111 + 3 → 1113
112 + 3 → 1123
122 + 3 → 1223
Count: 3 strings
Case 4: Prefix is 0 and Suffix is 3
Remaining Expression: 01+2∗3
Target Length: The 0 and 3 take up 2 characters. We need the 1+2∗ part to have a length of 2.
Combinations:
0 + 11 + 3 → 0113
0 + 12 + 3 → 0123
(Note: 0223 is not allowed because we must have at least one 1)
Count: 2 strings
Total Calculation
Summing up the counts from all cases:
4(Case 1)+3(Case 2)+3(Case 3)+2(Case 4)=12