Algorithm to check whether a word puzzle is valid