Reputation: 31
Can someone please tell me how to generate a regular expression for strings in which number of a's is a multiple of 3? The alphabet set is {a,b}.
I tried to construct a DFA for it first and then derive a RE from that. What I got was ((ba*)(ba*)(ba*))*.
Upvotes: 3
Views: 3634
Reputation: 1
For those asking the inverse DFA, i.e., accepts the language šæ={š¤ā{š,š}ā ā£ prevents the number of 'š's to be a multiple of 3}
the answer would be
bā+(bāabāabāab*)āa+(bāabāabāab*)āaa
This regular expression matches strings over the alphabet {a, b} where:
b*
represents zero or more occurrences of 'b'.(b*ab*ab*ab*)*a
represents zero or more occurrences of the pattern 'b', followed by one 'a', followed by zero or more 'b's, repeated three times, followed by one 'a'.(b*ab*ab*ab*)*aa
represents zero or more occurrences of the pattern 'b', followed by one 'a', followed by zero or more 'b's, repeated three times, followed by two 'a's, covering all possible combinations of non-multiple-of-3 'a's (3n+1,3n+2).
Upvotes: 0
Reputation: 522311
I would write the pattern as:
^b*(?:(?:ab*){3})*$
This matches:
^
from the start of the stringb*
optional leading b
zero or more times(?:
(?:ab*){3}
match a
followed by zero or more b
3 times)*
match this group zero or more times$
end of the stringUpvotes: 3