Finite state automata - probably not the right/best way. Will update.
Note