Remove the Substring (hard version)

CodeForces - 1203D2