an elegant solution for valid number problem which utilizes finite state machine.