Pregunta de entrevista de Meta

Array/string manipulation with optimization (maybe DP?) required to pass all the tests