I've done something like this when trying to count how many times you need each 'digit' to write the numbers 1 - 999999. I realized that it is very similar to a car's odometer, and proceeded from there.
Of course, the downside is that it doesn't find the string "ab" for the input "abcd", but that may not be part of your requirements.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
What does a metric clock look like? I bet it is nothing like this tiny ad: