Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Sum of all substrings of a number


//User function Template for Java

class Solution
{
    //Function to find sum of all possible substrings of the given string.
    public static long sumSubstrings(String s)
    {
    long sum=0;
    long mod=1000000007;
    int count=s.length();
    long one=1;
    while(count>0)
    {
       long number=((s.charAt(count-1)-'0')*count)%mod;
       long temp=(one*number)%mod;
       //System.out.println(temp);
       sum=(sum+temp)%mod;
       one=(one*10+1)%mod;
       count--;
    }
    return sum;
    
    }
}
    /*1111*count*number 
 

    222*2
    
  
    33*3
    


     4*count*/
Source by practice.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #Sum #substrings #number
ADD COMMENT
Topic
Name
9+4 =