Amazon interview question

Some dynamic programming on words.