DFA for the problem