CS402 Assignment No. 1
CS402 Assignment No. 1
Question 1
a. Write the regular expression for language consists of all strings that ends and have exact one
b and can have any number of a’s (excluding none) before it, over the set of alphabets Σ={a,b}.
Solution
The regular expression is
a+b
Solution a
a b
q0 q1 q2
Question 1
Consider the language L of strings, defined over ∑= {a, b}, Write the answers of the following:
a. If the regular expression is (𝑎𝑏)*, how many different strings of length 4 are in this
language?
Solution
This regular expression (ab)* represent zero or more repetitions of the string “ab”.
For strings of length 4:
1. Each “ab” contributes 2 characters.
2. To create a string of length 4, we need two repetitions of “ab”:
String “abab”
Number of strings of length 4 in this language:
1. 1(only”abab”).
b. If the regular expression is (𝑎∣𝑏)2, how many strings of length 2 can it generate?
Solution
This regular expression (𝑎∣𝑏)2 allows two characters where each character can
independently be either ‘a’ or ’b’. The | operator represents a choice between ‘a’ or ‘b’.
Total number of strings of length 2:
1. Each character has 2 options (‘a’ or ’b’).
2. For two positions, the total number of strings is: 2*2=4
Generated string
aa, ab, ba, bb
Number of strings of length 2:
4
c. If the regular expression is (𝑎𝑏∣𝑏𝑎)2, how many distinct strings of length 4 can be
generated?
Solution
This regular expression (𝑎𝑏∣𝑏𝑎)2 allows two repetitions of either “ab” or “ba”.
For strings of length 4:
1. Each repetition produces a 2-character substring (“ab” or “ba”).
2. There are 2 choices for each repetition: “ab” or “ba”.
3. For two repetitions the total number of strings is: 2*2=4
4. Generated strings:
“abab” (ab followed by ab)
“abba” (ab followed by ba)
“baab” (ba followed by ab)
“baba” (ba followed by ba)