Given a number as a string, no leading zeros, determine the sum of all integer values of substrings of the string.
Given an integer as a string, sum all of its substrings cast as integers. As the number may become large, return the value modulo 10**9+7.
Example:
n='42'
ans=4+2+42=48
Full problem: https://www.hackerrank.com/challenges/sam-and-substrings
My code:
def substrings(n):
ans=0
l=len(n)
for i in range(l):
ans+=int(n[i])*(i+1)*((10**(l-i)-1)//9)
return ans%(10**9+7)
It's currently getting timed out otherwise passing all test cases. Even though the problem category is dynamic programming, I don't understand how it could be used, and I'm not sure what's slowing my code down.
Answer Invalidation
, however, we don't explain other peoples code on code review. Please read What to do after the question has been answered. \$\endgroup\$