Meta Interview Question

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