how to solve java substring on hackerrank