J.P. Morgan interview question

About leetcode medium, some string operations