Moveworks Interview Question

leetcode hard string manipulation question.