Solution to SubstringDiff Hackerrank problem

(Needs improvement on running time)