And here is my line of reasoning when I first came upon it:
- This one seems difficult
- Finding a regular expression for it seems tricky, so i cannot follow this path to convert the regular expression to a DFA
- So I decided to solve the first part of the problem: Accept a string whose number of 'a' is a multiple of 3. This is quite easy, you just need 3 states: 1,2,3 with 1 as the start state, if there is an input 'a', you go to the next one, if it's 'b' you stay at that state, and the start state is also the finishing state. But here in this exercise, these 3 states would actually be 3 'big' states. Then the problem becomes designing the internals of these 'big states and how they interact.
I had no choice but to use guesswork in order to arrive at a solution. Here is what I came up with (1,2,3 are finishing states, 3 is the start state, and also 7 and 9 both goes to 1 if an input 'a' is received): I also used DFA minimization on this one, and found out that it is already minimal. However, the textbooks gives another solution:
But I don't understand how it is correct! I just can't figure it out :(. And I don't even know if my solution is 100% correct. Please help me, thank you very much :)
Your answer seems to be correct. I haven't carefully proved it, but the logic is fairly clear. Furthermore, I wrote a Python program to test it:
The operation of the function
accepts
is explained in this answer. I tailored it to match your diagram. To stress-test it I generated 100,000 random inputs whose length range from 1 to 1000 and then compared the output from the DFA with a direct verification of the condition. Every time I run this code, the output is a satisfyingTrue
.To test individual strings: