Algorithm to find the words