finite automate solution