Let's do some “enciph5r47g”
Retina, 24 23 bytes

(.)(?&lt;=\1(.{0,9}).)
$.2


Try it online!

A fairly simple regex substitution. We match each character and try to find a copy of it 0-9 characters before it. If we find it, we replace the character with the number of characters we had to match to get to the copy.

The results don't quite match the test cases, because this one uses the largest possible digit instead of the smallest possible one.