Sum of all substrings of a number | Practice | GeeksforGeeks

Given an integer S represented as a string, the task is to get the sum of all possible sub-strings of this string.
As the answer will be large, print it modulo 10^9+7.
Example 1:
Input:
S = 1234
Output: 1670
Explanation: Sum = 1 + 2 + 3 + 4 + 12 +
23 + 34 + 123 + 234 + 1234 = 1670

Example 2:
Input:
S = 421
Output: 491
Explanation: Sum = 4 + 2 + 1 + 42 + 21
Your Task:
You only need to complete the function sumSubstrings that takes S as an argument and returns the answer modulo 1000000007.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= |S| <= 104
0
vinamrajha1 week ago
long long sumSubstrings(string s){
       // your code here
       long long n = s.length();
       long long dp[n],sum=0, d = 1e9+7;
       dp[0] = (s[0]-48)%d;
       for(long long i =1; i<n; ++i)dp[i] = ((i+1)*(s[i]-48)+10*dp[i-1])%d;
       for(long long i =0; i<n; ++i) sum = (sum+dp[i])%d;
       return sum;
   }
0
mdzeya3 months ago
//java code using dynamic programming
class Solution
{
    //Function to find sum of all possible substrings of the given string.
    public static long sumSubstrings(String s)
    {
    //Your code here
    long sum[]=new long[s.length()];
    long a=Character.getNumericValue(s.charAt(0));
    sum[0]=a;
    long result=sum[0];
    //sumofdigit[i] = (i+1)*num[i] + 10*sumofdigit[i-1]
    for(int i=1;i<s.length();i++)
    {
        long k=Character.getNumericValue(s.charAt(i));
        sum[i]=(i+1)*k+10*sum[i-1]%1000000007;
        result+=sum[i]%1000000007;
    }
    return  result%1000000007;
}}
-9
sumit dutta4 months ago
Naive Solution : long long sumSubstrings(string s){       // your code here       int n = s.length();       int mod = 1000000007;       int res=0;       for(int i=0 ; i<n ; i++)       {           string curr = "";           for(int j=i ; j<n ; j++)           {               curr += s[j];               res += stoi(curr);           }       }       return res%mod;   }
-1
raghvan084 months ago
dp solution
long long sumSubstrings(string s){              // your code here       int n=s.size();       long long dp[n];       dp[0]=(s[0]-'0')%1000000007;       long long sum=dp[0];       for(int i=1;i<s.size();i++)       {        dp[i]=((i+1)*(s[i]-'0')+10*dp[i-1])%1000000007;           sum=(sum+dp[i])%1000000007;       }       return sum%1000000007;   }
0
chakresh245 months ago
public static long sumSubstrings(String s)
    {
        
        int n = s.length();
        long [] dp = new long[n];
        long m = 1000000007;
        dp[0] = ((long)s.charAt(0))-48;
        for(int j = 1;j<n;j++)
        dp[j] = (dp[j-1]*10+(j+1)*(((long)s.charAt(j))-48))%m;
        
        long sum = 0;
        for(int i=0;i<n;i++){
            sum = (sum+dp[i])%m;
        }
        return sum;
    }
 
 
0
abc123programming6 months ago
JAVA SOLUTION:
class Solution{   //Function to find sum of all possible substrings of the given string.   public static long sumSubstrings(String s)   {       int n=s.length();       long dp[]=new long[n];       dp[0]=s.charAt(0)-'0';       long res=dp[0];              for(int i=1;i<n;i++){           dp[i]=(((i+1)*(s.charAt(i)-'0')) + (10 * dp[i-1])%1000000007);           res = (res+dp[i])%1000000007;       }       return res%1000000007;   }}
0
iamrbkfarhan7 months ago
 
class Solution
{
    public:
    //Function to find sum of all possible substrings of the given string.
    long long sumSubstrings(string s){
        
        // your code here
          long long prev_res = s[0] - '0';
    long long res = prev_res % 1000000007;
    long sub_res;
    
    for(int i = 1; i < s.size(); i++)
    {
        sub_res = ((i+1)*(s[i] - '0') + 10 * prev_res)%1000000007;
        
        res = (res + sub_res)% 1000000007;
        prev_res = sub_res;
    }
    
    return res;
    }
};
0
armanyppph
This comment was deleted.
0
BHOOPEN SAWANT9 months ago
BHOOPEN SAWANT
Correct C++ soln -  https://uploads.disquscdn.c...
0
Anonymous9 months ago
Anonymous
Recursive Approach : https://ide.geeksforgeeks.o...
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.