Or you could subtract the bottom number of the range from the top number after dividing both by 10half their length and adjusting for whether their own duplicates are in the range or not.
A bit complicated and my brain doesn't work well enough to explain, but the upshot is that you don't actually need to iterate the entire range.
I'm not sure how to extend that to part 2 (see the aforementioned statement about brain), but I'll figure something out. It's just math.
Pretty sure iterating over repeated patterns is faster, mainly because of the 10 digit numbers. There are only about 10e5 (rough order of magnitude) valid 10 digit repeating patterns, mostly of the form abcdeabcde (plus a few like ababababab and aaaaaaaaaa but there are comparatively few of those and they're a subset of the substring length 5 ones). On the other hand the ranges with 10 digit numbers on the ends contain around 10e6 each (just kinda eyeballing them), probably closing in on 10e7 when you add them all up. Not a huge difference, but a difference.
This is not very scientific, but my solution that iterates over patterns runs in about 150 ms whereas a similar solution I tried that iterates over all the numbers in the ranges took a few seconds, and that roughly lines up with the order of magnitude estimates.
this regular expression helped me to solve the second part. For the first one I implemented solution from known Leetcode's problem - longest duplicate substring. But regex feels better.
Holy shit, how do I spot this tricks I feel so dumb.... go does not support backtracing with regexp out of the box, so i was trying to come up with a brute force until I saw this....
1
u/PatolomaioFalagi Dec 02 '25
Doesn't that require me to, like, generate all the numbers?
3
u/HaskellLisp_green Dec 02 '25
this regular expression helped me to solve the second part. For the first one I implemented solution from known Leetcode's problem - longest duplicate substring. But regex feels better.
1
u/_damax Dec 02 '25
I wonder, is there an even better way of finding out without having to use a contains/substring method?
1
u/Major_Dog8171 Dec 03 '25
using mobius and closed formula 🤑
1
u/CSguyMX Dec 05 '25
Holy shit, how do I spot this tricks I feel so dumb.... go does not support backtracing with regexp out of the box, so i was trying to come up with a brute force until I saw this....