Finite Automata

Answer & solution
Official answer: 4
Step through it (2 parts)
Use three accepting states tracking the trailing run of ones of lengths 0, 1, 2, and a rejecting trap after the third consecutive one. A zero resets each nontrap state to run length 0; a one advances the run or reaches the trap. All four states are reachable. The trap differs from the others on the empty suffix. For two run lengths i<j among 0, 1, 2, append 3-j ones: the state j rejects but state i still accepts. Thus all four are pairwise distinguishable and the DFA is minimal.
Teaching detail. Name states q0,q1,q2,dead by the count of trailing 1s until a forbidden run has occurred. On 0, q0,q1,q2 go to q0; on 1 they go to q1,q2,dead respectively. dead loops on both symbols because an earlier occurrence of 111 can never be undone by later zeros. q0,q1,q2 accept, including q0 for ε. Reachability witnesses are ε,1,11,111. A construction alone proves an upper bound of four; the distinguishability argument is required to exclude every possible smaller DFA.

