Say, you have string "12345". Say, i = 4 (that is, tf.charAt(i) == '5').
Now look at the code that checks all the substring:
for (int k=0;k<i;k++)
I've simplified it a bit to make it more readable, but this is essentially the same thing.
We have a loop. It checks each number before current. If it is distinct, it prints current number. In our case, all 4 numbers are distinct from '5'. Hence, '5' will be printed 4 times. Not something you want.