Building Regular Expressions - University Questions
1. Find a regular expression for the set of positive integers in traditional decimal notation.
Answer:
2. Find a regular expression for L = { aⁿ, n ≥ 2 } ∪ { bⁿ, n ≥ 1 }.
Answer:
4. How many strings of length eight are there in L((aa + bb) )*
Answer:
Each unit is aa or bb, i.e., 2 characters per unit.
Length 8 → 4 such units → total combinations = 2⁴ = 16
16 strings
5. Which of the following strings are in L((aa + b) )*
-
(i) aabbbbaa
-
(ii) abbbba
-
(iii) aaabbbaaa
Answer:
Only (i) is in the language.
6. Is L((aa + b)) the same as L((b + aa))?
Yes — order in union doesn't matter.
Answer:
✅ Yes, they define the same language.
7. Is L((0+ 1))* is same as L(0*+1*))? give example
Step-by-Step Comparison
🔹 L(r₁) = L((1 + 0)*)
This is:
-
All strings over
{0, 1}. -
Includes any combination, in any order.
-
Examples:
ε,0,1,01,10,0011,1010, etc.
✅ Accepts all binary strings (i.e., Σ* where Σ = {0, 1}).
🔹 L(r₂) = L(0* + 1*)
This is:
-
All strings of only 0s, like
ε,0,00, ... -
Or all strings of only 1s, like
ε,1,11, ...
⛔ Does not include mixed strings like 01, 10, 1100, etc.
❌ Conclusion: Not Equivalent
-
L((1 + 0)*)includes all binary strings. -
L(0* + 1*)includes only unary strings (only 0s or only 1s).
Example to show they differ:
-
String
"01"∈ L((1 + 0)*) ✅ -
String
"01"∉ L(0* + 1*) ❌
8. Prove that L(r₁ + r₂) = L(r₂ + r₁), but L(r₁r₂) ≠ L(r₂r₁) in general.
Answer:
-
Union is commutative:
r₁ + r₂ = r₂ + r₁ -
Concatenation is not:
ab ≠ ba
Example:
-
r₁ = a, r₂ = b
-
L(r₁r₂) = ab
-
L(r₂r₁) = ba
L(r₁ + r₂) = L(r₂ + r₁)
L(r₁r₂) ≠ L(r₂r₁)
More Questions
1. Q: Strings over {0,1} that contain exactly one 1
A: 0*1 0*
Explanation: Any number of 0s before and after exactly one 1.
2. Q: Strings over {a, b} that start and end with the same symbol
A: (a(a + b)*a) + (b(a + b)*b) + a + b
Explanation: Starts and ends with a or b, including length-1 strings.
✅ Explanation:
-
a(a + b)*a: Strings that start and end witha, with any sequence ofaorbin between (including empty). -
b(a + b)*b: Strings that start and end withb, similarly. -
a + b: To include length-1 strings likeaorb.
3. Q: Strings over {0,1} where every 0 is immediately followed by 1
A: (1 + 01)*
Explanation: Only 1s or 0s followed by 1.
4. Q: Strings over {a, b} with an even number of a's
A: ((b*ab*a)*b*)*
Explanation: Pairs of a's surrounded by any number of b's.
✅ Explanation:
Let’s break it down:
-
b*→ Any number ofbs before the firsta. -
ab*ab*→ A pair ofas, possibly separated bybs. -
*on the outer group → Repeats of these pairs ensure even count ofas.
Each ab*ab* contributes two as, ensuring the total number is even.
🔁 Examples of accepted strings:
-
""→ empty string (0 a's → even ✅) -
bbbbb→ no a's → even ✅ -
bababb→ twoas → even ✅ -
baababbbab→ fouras → even ✅
❌ Examples of rejected strings:
-
a→ onea→ odd ❌ -
abba→ twoas but not following the pattern ofab*ab*❌ -
ababa→ threeas ❌
5. Q: Strings over {0,1} that do not contain 11
A: (0 + 10)*1?
To construct a regular expression for strings over {0,1} that do not contain the substring 11, we must allow any sequence of 0s and isolated 1s, but never two 1’s in a row.
✅ Regular Expression:
✅ Explanation:
-
(0 + 10)*:-
Allows:
-
0→ a standalone 0. -
10→ a 1 that is not followed by another 1 (since 1 is followed by 0).
-
-
-
(1)?:-
Optionally allows a single
1at the end.
-
This ensures no two consecutive 1s can appear in the string.
✅ Examples of accepted strings:
-
""(empty string) -
0,1,010,1010,01010 -
000100010✅ (no11)
❌ Examples of rejected strings:
-
11 -
0110(contains11) -
1011(contains11)
6. Q: Strings over {a, b} that contain at least one a and end in b
A: (a + b)*a(a + b)*b
Explanation: Ensure at least one a and ending in b.
7. Q: Strings over {0,1} that begin with 10
A: 10(0 + 1)*
Explanation: Fixed prefix followed by any string.
8. Q: Strings over {a, b} where a appears at least twice
A: (a + b)*a(a + b)*a(a + b)*
Explanation: Two or more as anywhere.
9. Q: Binary numbers divisible by 4 (i.e., ending in 00)
A: (0 + 1)*00
Explanation: Binary numbers ending with two 0s.
10. Q: Strings over {x, y} that do not contain yy
A: (x + yx)*y?
To build a regular expression for strings over the alphabet {x, y} that do not contain the substring yy, we must ensure that no two ys occur consecutively.
✅ Regular Expression:
✅ Explanation:
-
(x + yx)*:-
Allows:
-
x: any number ofxs. -
yx: aythat is followed by anx, which preventsyy.
-
-
-
(y)?:-
Optionally allows a single
yat the end, which is safe (not followed by anothery).
-
Together, this regular expression generates all strings where y never appears twice in a row.
✅ Examples of accepted strings:
-
""(empty string) -
x,y,xy,xyx,xxyxxyx,yxyx,xxyx
❌ Examples of rejected strings:
-
yy -
xyy -
xyyx,xxyyyx
These contain yy as a substring.
Comments
Post a Comment