Pregunta de entrevista de Amazon

Some dynamic programming on words.