04. Sum of all substrings of a number
My Approach
Time and Auxiliary Space Complexity
Code (C++)
class Solution {
public:
long long sumSubstrings(string s){
long long sum = 0;
int mod = 1e9+7;
int n = s.size();
long mul = 1;
for (int i = n - 1; i >= 0; --i) {
sum += ((s[i] - '0') * mul * (i + 1)) % mod;
sum %= mod;
mul = (mul * 10 + 1) % mod;
}
return sum;
}
};Contribution and Support
Last updated