How to approach for problem - SPCLPN

Can anybody provide approach to solve : SPCLPN

I checked a few solutions but didn’t get how to solve it. Many have solved it using DP but I was unable to understand the optimal substructure and the recursion.

My approach used a recursive system of finding the frequency of various digit differences over ranges of numbers, trading on the common higher digits to find those difference values once rather than repeatedly.

my code

I am not understanding your code, specially this part " {dlo-i : 1 for i in range(lom, him+1)} ".
I’m not very good at python.If u could write a c++ code that would be great.