Moveworks interview question

leetcode hard string manipulation question.